2013-03-01 75 views
-6

我有一个问题。如何从MyControl(按钮)获取按钮文本?

用户控件

Button : UserControl 
... 
public string naz 
{ get { return this.button1.Text; } } 
... 

在我的形式,我可以做到这一点

if(button0.naz == "1"){ MessageBox.Show("My Text"); } 
if(button1.naz == "1"){ MessageBox.Show("My Text"); } 
if(button2.naz == "1"){ MessageBox.Show("My Text"); } 

但是,当我尝试以下方法.naz无法识别。

for(int i=0;i<=60;i++) 
{ 
    if(this.Controls["button" + i.ToString()).naz == "1") 
    { 
     MessageBox.Show("My Text"); 
    } 
} 

回答

0

您需要将控件转换为用户控件的类型。如果你的班级真的叫Button那么类似这样的

var myButton = this.Controls["button" + i] as Button; 
if(myButton != null && myButton.naz == "1") 
...