views:

243

answers:

2

Hi, I'm 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 think that is what they are called), but I will know the index of which one was clicked on. I know how to do this in VB and C#, but I can't seem to figure it out with C++ Right now I have all my buttons labelled by their location, i.e. b00, b10, b21, etc. So I think what I am 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 appreciated. Also let me know if I am going in the complete wrong direction with this. Thanks!

+1  A: 

Use a cli::array to store an array of a CLI type. For example, to create an 8x8 two-dimensional 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 about cli::array.

James McNellis
Thanks for the response! I don't think it will let me create it globally though, because I am getting this error (error C3145: 'b' : global or static variable may not have managed type 'cli::array<Type,dimension> ^')If I declare it in Main() it works fine, but I don't think I will be able to use it, since the handler will not have access to it, or is there a way to get it to work still? (I'm a complete noob at visual C++ btw, never used it before in my life)
Hint: you're not using C++ at all. It's C++/CLI, which, despite many similarities to C++, is a different language. In any case, managed objects cannot be global, because...well, it just doesn't make any sense. You can't do that in any .NET language, because every object has to be part of a class. In any case, you probably don't have to have access to the object anyway. Beyond that: Why are you using C++/CLI at all? C# is much cleaner for doing .NET stuff, so unless you are frequently interacting with some unmanaged library (enough so that P/Invoke is burdensome), you might as well use C#.
Travis Gockel
Yea, I'm thinking C# would be much easier. The reason I'm trying to do it in C++ is because I am trying to get better at the language, but I guess I should probably try something that only has a command line interface, to save me from the many problems I am running into
@mangoman13: If you want to learn C++, I would strongly recommend not messing with C++/CLI. C++/CLI confuses many aspects of C++ programming and the list of gotchas is very long. If you want to learn C++, there are other GUI toolkits available that you can use.
James McNellis
A: 

You don't need an array for this. Wire all the buttons up to the same event handler function, then parse the coordinates from the sender's name.

Marcelo Cantos