如何在C ++中创建一个按钮数组

时间:2010-04-25 03:24:09

标签: .net c++-cli control-array

我在VS2005中使用C ++,并在表单上有一个8x8的按钮网格。我希望将这些按钮放在一个数组中,所以当我点击它们中的任何一个时,它将打开相同的事件处理程序(我认为这就是它们被称为),但我将知道单击哪一个的索引。我知道如何在VB和C#中做到这一点,但我似乎无法用C ++来解决这个问题 现在我的所有按钮都标有他们的位置,即b00,b10,b21等。所以我认为我正在寻找的是一种方法来做这样的事情:

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;
...
}

然后在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
}

任何帮助将不胜感激。如果我正朝着完全错误的方向前进,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:3)

使用cli::array存储CLI类型的数组。例如,要创建一个8x8二维数组,就像您的问题一样,您可以使用:

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

See MSDN for more information about cli::array.

答案 1 :(得分:1)

你不需要一个数组。将所有按钮连接到相同的事件处理函数,然后解析发件人姓名中的坐标。