C# 循环遍历给定标签的数组
对我来说描述起来有点困难,但是这个伪 C# 应该可以解释我想要做什么。
Windows 窗体上有大量标签。
我想更改其中一些标签的文本颜色。
private void allBlackLabels()
{
int[] lray = { 1, 2, 3, 5, 6 };
foreach (int i in lray)
{
label[i].Forecolor = Color.Black;
}
}
我希望这能解释我在这里尝试做的事情。
现在很明显,由于标签[i],它不起作用,但我该如何解决这个问题呢?
It's a bit difficult for me to describe, but this pseudo C# should explain what I'm trying to do.
There is a large number of labels on a windows form.
I'd like to change the text colour of some of those labels.
private void allBlackLabels()
{
int[] lray = { 1, 2, 3, 5, 6 };
foreach (int i in lray)
{
label[i].Forecolor = Color.Black;
}
}
I hope that explains what I am trying to do here.
Now it's obvious it won't work due to the label[i], but how would I get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
它可能不起作用,因为您的标签未保存在数组中。考虑到您知道要更改的标签的名称,以下代码将起作用:
It might not work, because your Labels aren't held in an array. The following code will work, considering you know the names of the label to be changed:
如果您实际上有一组标签,那就可以了。
如果你只有很多变量,就像这样:
那就更难了。选项:
Controls.Find
与每个 IDControls
以查找所有Label
控件,无论名字的That would work fine if you actually had an array of labels.
If you've only got lots of variables, like this:
then it's harder. Options:
Controls.Find
with each IDControls
to find allLabel
controls, regardless of name这将获取表单的所有子控件(前提是此代码位于表单的代码隐藏中)并检查它们是否是标签。只需进行任何您想要的操作来代替评论即可。
That'll grab all of the form's child controls (provided this code is in the form's code-behind) and check to see if they're labels. Just do any manipulation you want in place of the comment.
您可以自己创建数组。不过,每次表单更改时,这都会涉及一些维护成本/风险。注意:您可能希望将数组创建位放在 Form Loaded 事件中,而不是构造函数中,或者至少在
InitializeComponent
调用之后,以便设置控件。或者,您可以迭代 Form 的所有子项(来自 Form 方法的
this.Controls
),使用特定标签标记您的标签,或者获取所有标签(如果这是您的目的)。请注意,如果表单中有许多控件,第一个解决方案在运行时性能方面可能要快得多。
You could create the array yourself. This involves a bit of maintenance cost/risk each time your form changes though. Note: you probably want to put the array creation bit in a Form Loaded event, not a constructor, or at least, after the
InitializeComponent
call, so your controls are setup.Or you could iterate on all the children of your Form (
this.Controls
from a Form method), mark your labels with a specific Tag, or get all Labels if that's your purpose here.Please note that the first solution is probably much faster in runtime performance terms if you have many controls in your form.
如果您想对表单上的所有标签实现此目的,那么类似这样的事情可能会有所帮助:
但是,如果您只需要更改所有标签的子集,那么您需要存储需要更改的标签的名称或索引。这是因为
this.Controls
有两个索引器:一个 int32 索引器和一个 string 索引器。因此,你可以尝试这样的事情,但是值得注意的是,
this.Controls
中的排序很可能不像你的lray
数组那样线性。希望这有帮助
If you want to achieve this for all labels on the form then something like this may help:
However if you only need to change a subset of all the labels then you need to either store the names or the indexes of the labels which need changing. This is because
this.Controls
has two indexers, an int32 and a string one. Thus you could try something like thisIt is defiantly worth noting however that the ordering in
this.Controls
will most likely not be as liniear looking as yourlray
array.Hope this helps
尝试这样的事情:
希望这会有所帮助。
Try some thing like this:
Hope this helps.
我注意到您在示例中从“lray”数组中排除了 4 。如果您希望排除一个或多个标签不被代码自动更新(也许是为了突出显示一组标签中的一个),请尝试此操作。
I noticed that you excluded 4 from the "lray" array in your example. If you are looking to exclude one or more labels from being automatically updated by code (perhaps to highlight one among a group), try this.
在伪 C# 中:
in pseudo c#: