在事件處理程序,你有事件的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);
}
哇,這是一個地獄很多按鈕和事件處理的! – Goodwine 2013-05-08 20:46:49