在您的事件处理程序中,您有sender
事件的:
void button_Click(Object^ sender, EventArgs^ e)
{
// This is the name of the button
String^ buttonName = safe_cast<Button^>(sender)->Name;
}
如果您需要项目的索引(行和列),则需要遍历数组,因为Array::IndexOf
不支持多维数组。让我们(在某处)编写一个像这样的通用函数:
static void Somewhere::IndexOf(Array^ matrix, Object^ element, int% row, int% column)
{
row = column = -1;
for (int i=matrix->GetLowerBound(0); i <= matrix->GetUpperBound(0); ++i)
{
for (int i=matrix->GetLowerBound(1); i <= matrix->GetUpperBound(1); ++i)
{
// Note reference comparison, this won't work with boxed value types
if (Object::ReferenceEquals(matrix->GetValue(i, j), element)
{
row = i;
column = j;
return;
}
}
}
}
所以最后你可能有这个:
void button_Click(Object^ sender, EventArgs^ e)
{
// This is the name of the button
String^ buttonName = safe_cast<Button^>(sender)->Name;
// This is the "location" of the button
int row = 0, column = 0;
Somewhere::IndexOf(button, sender, row, column);
}