在运行时使用c#启用一系列序列命名的Label控件 [英] Enable at runtime a series of serially named Label controls using c#

查看:109
本文介绍了在运行时使用c#启用一系列序列命名的Label控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些标签控件,其名称如下:
Label1,Label2,Label3 ....... Label14,在设计时禁用。
现在我想以以下方式使用for循环启用它们

I have some label control named as follows : Label1, Label2, Label3.......Label14, disabled at design time. Now i want to enable them using a for loop in the following manner

for(int i=1;i<15;i++)
{
    (Label+i).Enabled = true;
}

显然,以上代码无法编译。

Obviously the above code does not compile.

Label1.Enabled = true;
Label2.Enabled = true;
Label3.Enabled = true;
...........
Label14.Enabled = true;

除了上面提到的方法以外,还有其他方法可以达到预期的效果吗?我自己清楚。请提供代码建议。谢谢

Is there any other way of achieving the desired result other than the way mentioned just above.Hope i make myself clear.Please advice with code.Thanks in advance

推荐答案

您可以从父母的中按名称获取控件。控件集合

for(int i = 1; i < 15; i++)
{
    Control label = this.Controls["Label"+i];
    if (label != null) 
        label.Enabled = true;
}

在此示例中, this 是指表格。如果标签属于另一个父控件(例如Panel或GroupBox),则应使用它代替 this

In this example this refers to the form. If labels belong to another parent control (e.g. Panel or GroupBox) it should be used instead of this

这篇关于在运行时使用c#启用一系列序列命名的Label控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆