2016-12-21 50 views
0

这是我的Form1.cs文件。该方法应该显示一个MessageBox点击按钮的ID,但我得到这个错误。按钮不包含定义ID

private void button_Click(object sender, EventArgs e) 
     { 

      Button button = sender as Button; 
      string buttonId = button.ID; 
      MessageBox.Show(buttonId); 
     } 

错误:

'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?)

+2

这是一个ASP.NET网站或Windows应用程序? – Nirman

+0

你使用了正确的命名空间吗? – Prabu

+0

这是一个Windows应用程序。 – Alex

回答

0

如果这是一个Windows窗体应用程序,则建于Button控件没有一个叫ID属性。您可能想要获取Name属性,该属性对于每个按钮控件都是唯一的。

一个简单的例子:

private void button_Click(object sender, EventArgs e) 
{ 
    Button button = sender as Button; 
    string buttonName = button.Name; //Button does not have an ID - use Name instead 
    MessageBox.Show(buttonName); 
} 
+0

是的,我可以做到这一点。但我想获得按钮的订单号。 – Alex

+0

你的订单号码是什么意思?你可以尝试使用'button.TabIndex'属性,但是这个值对每个按钮都不是唯一的。 –

+0

因此,当我点击'button1'时,我想要得到数字1.当我点击'button2',数字2等... – Alex

相关问题