本文介绍了在代码中使用对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我想知道如何在代码中使用对象名称。
示例我有多个标签命名为lbl001,lbl002等。
在我的代码中,如果我想更改所有标签文本,我将如何轻松更改它们?
ie
Hi all
I was wondering how you would go about using an object name in the code.
Example i have a number of labels naming lbl001, lbl002 etc.
In my code, say if i want to change all the label text, how would i change them easily?
ie
for (int 1=0;i<20;i++)
{
lbl + i.tostring().padleft(0,3) .text - "Hello world";
}
TIA
TIA
推荐答案
foreach(Control ctl in this.Controls)
{
if(ctl.GetType()==typeof(Label))
{
ctl.Text="Hello World";
}
}
谢谢
Magesh.M
Thanks
Magesh.M
foreach (Control c in parent.Controls)
{
int cnt = 1;
if (c.GetType() == typeof(Label)) {
if (c.Name == "lbl" + cnt)
{
//Do Stuff with the control
cnt++;
}
}
}
这篇关于在代码中使用对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!