遍历给定标签的数组

遍历给定标签的数组

本文介绍了C#遍历给定标签的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难形容,但是这个伪C#应该可以解释我要做什么.

It's a bit difficult for me to describe, but this pseudo C# should explain what I'm trying to do.

在Windows窗体上有大量标签.

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.

现在很明显由于标签[i]而无法使用,但是我将如何解决呢?

Now it's obvious it won't work due to the label[i], but how would I get around this?

推荐答案

它可能不起作用,因为您的Labels不在数组中.考虑到您知道要更改的标签的名称,以下代码将起作用:

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:

Label[] lray = { labelOne, labelDifferent, labelWhatElse };
foreach (Label label in lray)
{
    label.ForeColor = Color.Black;
}

这篇关于C#遍历给定标签的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:54