How to create an array of buttons in C ++

I am using C ++ in VS2005 and have an 8x8 grid of buttons on a form. I want to have these buttons in an array, so when I click on any of them it will open the same event handler (I guess that's what they are called), but I find out the index that was clicked. I know how to do this in VB and C #, but I can't figure it out with C ++ Right now I have all my buttons labeled with their location, i.e. b00, b10, b21, etc. So I think what I'm looking for is a way to do something like this:

Button b[8][8]; //this causes me errors (error C2728: 'System::Windows::Forms::Button' : a native array cannot contain this managed type) and (error C2227: left of '->{ctor}' must point to class/struct/union/generic type)   

void Assignment(){
b[0][0] = b00;
b[1][0] = b10;
...
}

      

and then in form1.h:

private: System::Void b_Click(System::Object^  sender, System::EventArgs^  e) {
//somehow read the coordinates into variables x and y
//do something based on these values
}

      

Any help would be greatly appreciated. Also let me know if I go with a complete wrong direction. Thanks!

+2


a source to share


2 answers


Use cli::array

CLI type to store array. For example, to create an 8x8 2D array like in your question, you can use:

cli::array<Button^, 2>^ b = gcnew cli::array<Button^, 2>(8, 8);

      



See MSDN for more information on cli::array

.

+3


a source


You don't need an array for this. Route all buttons to the same event handler function, then parse the coordinates on behalf of the sender.



+1


a source







All Articles