This is my Form1.cs
file. The method should display by a MessageBox
the clicked button's ID, but I get this error.
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
string buttonId = button.ID;
MessageBox.Show(buttonId);
}
Error:
'Button' does not contain a definition for 'ID' and no extension method 'ID' accepting a first argument of type 'Button' could be found (are you missing a using directive or an assembly reference?)
If this is a Windows Forms application, then the built in
Button
controls do not have a property calledID
. You probably want to fetch theName
property, which is unique for each button control.A quick example: