【问题标题】:add variable to name label in C#在 C# 中将变量添加到名称标签
【发布时间】:2016-03-27 17:58:57
【问题描述】:
int j = 1;
        label+j.Text = "";

变量名标签后显示错误

Error   1   The name 'label' does not exist in the current context  

如何在 C# 中使用它?

【问题讨论】:

    标签: c# oop variables object


    【解决方案1】:

    使用Find 方法,您可以在运行时通过其名称获得任何控制:

    Label lbl = this.Controls.Find("label"+j, true).FirstOrDefault() as Label;
    lbl.text = "";
    

    它返回所有可能控件的集合,但我们只需要一个(第一个),如果它的名称是唯一的。

    【讨论】:

      【解决方案2】:

      如果您的控件名称是label,那么您可能想要这样做

          this.label.Text = string.Empty;
      

      【讨论】:

        猜你喜欢
        • 2013-04-28
        • 2021-04-28
        • 2013-09-11
        • 1970-01-01
        • 2013-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多