【问题标题】:How get button Text from MyControl(Button)? [closed]如何从 MyControl(Button) 获取按钮文本? [关闭]
【发布时间】:2013-02-15 22:32:15
【问题描述】:

我有问题。

用户控制

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"); 
    }  
}

【问题讨论】:

    标签: c# get controls set


    【解决方案1】:

    您需要将控件转换为用户控件的类型。如果你的班级真的被称为Button,那么就像这样

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

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2012-08-07
      • 1970-01-01
      相关资源
      最近更新 更多