C# 循环遍历给定标签的数组

发布于 2024-12-10 17:07:37 字数 353 浏览 0 评论 0原文

对我来说描述起来有点困难,但是这个伪 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

千柳 2024-12-17 17:07:37

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

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

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;
}            
大姐,你呐 2024-12-17 17:07:37

如果您实际上一组标签,那就可以了。

如果你只有很多变量,就像这样:

private Label label1;
private Label label2;
private Label label3;
private Label label4;
private Label label5;

那就更难了。选项:

  • 更改代码以改用数组。设计器中不太好,但更符合逻辑
  • 使用 Controls.Find 与每个 ID
  • 迭代 Controls 以查找所有 Label 控件,无论名字的

That would work fine if you actually had an array of labels.

If you've only got lots of variables, like this:

private Label label1;
private Label label2;
private Label label3;
private Label label4;
private Label label5;

then it's harder. Options:

  • Change the code to use an array instead. Not as nice in the designer, but more logical
  • Use Controls.Find with each ID
  • Iterate over Controls to find all Label controls, regardless of name
始于初秋 2024-12-17 17:07:37
foreach (Control c in this.Controls)
{
    if (c is Label)
    {
        // do stuff
    }
}

这将获取表单的所有子控件(前提是此代码位于表单的代码隐藏中)并检查它们是否是标签。只需进行任何您想要的操作来代替评论即可。

foreach (Control c in this.Controls)
{
    if (c is Label)
    {
        // do stuff
    }
}

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.

烟雨凡馨 2024-12-17 17:07:37

您可以自己创建数组。不过,每次表单更改时,这都会涉及一些维护成本/风险。注意:您可能希望将数组创建位放在 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.

西瑶 2024-12-17 17:07:37
private void allBlackLabels()
{
    int[] lray = { 1, 2, 3, 5, 6 };
    foreach (int i = 0; i < lray.Length; i++)
    {
        ((Label)this.Controls["label" + i]).ForeColor = Color.Black;
    }            
}
private void allBlackLabels()
{
    int[] lray = { 1, 2, 3, 5, 6 };
    foreach (int i = 0; i < lray.Length; i++)
    {
        ((Label)this.Controls["label" + i]).ForeColor = Color.Black;
    }            
}
痞味浪人 2024-12-17 17:07:37

如果您想对表单上的所有标签实现此目的,那么类似这样的事情可能会有所帮助:

foreach (Control control in this.Controls) {
     if (control is Label) {
         (control as Label).Forecolor = Color.Black;
     }
}

但是,如果您只需要更改所有标签的子集,那么您需要存储需要更改的标签的名称或索引。这是因为 this.Controls 有两个索引器:一个 int32 索引器和一个 string 索引器。因此,你可以尝试这样的事情,

private void allBlackLabels()
{
    int[] lray = { 1, 2, 3, 5, 6 };
    foreach (int i in lray)
    {
         this.Controls[i].Forecolor = Color.Black;
    }            
}

但是值得注意的是,this.Controls 中的排序很可能不像你的 lray 数组那样线性。
希望这有帮助

If you want to achieve this for all labels on the form then something like this may help:

foreach (Control control in this.Controls) {
     if (control is Label) {
         (control as Label).Forecolor = Color.Black;
     }
}

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 this

private void allBlackLabels()
{
    int[] lray = { 1, 2, 3, 5, 6 };
    foreach (int i in lray)
    {
         this.Controls[i].Forecolor = Color.Black;
    }            
}

It is defiantly worth noting however that the ordering in this.Controls will most likely not be as liniear looking as your lray array.
Hope this helps

晚风撩人 2024-12-17 17:07:37

尝试这样的事情:

List<string> ListLabelNames = new List<string>() { /* Your label name list*/ };

foreach (Label TmpLabel in this.Controls.OfType<Label>())
{
    foreach (string strTmp in ListLabelNames)
    {
        if (String.Compare(TmpLabel.Name, strTmp, true) == 0)
            TmpLabel.ForeColor = System.Drawing.Color.Black;
    }
}

希望这会有所帮助。

Try some thing like this:

List<string> ListLabelNames = new List<string>() { /* Your label name list*/ };

foreach (Label TmpLabel in this.Controls.OfType<Label>())
{
    foreach (string strTmp in ListLabelNames)
    {
        if (String.Compare(TmpLabel.Name, strTmp, true) == 0)
            TmpLabel.ForeColor = System.Drawing.Color.Black;
    }
}

Hope this helps.

要走干脆点 2024-12-17 17:07:37

我注意到您在示例中从“lray”数组中排除了 4 。如果您希望排除一个或多个标签不被代码自动更新(也许是为了突出显示一组标签中的一个),请尝试此操作。

    private void allBlackLabels()
    {    
        foreach (Control control in this.Controls)            
        {
            if (control is Label && control.Name != "label4")
            {
                (control as Label).Forecolor = Color.Black;
            }
        }
    }

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.

    private void allBlackLabels()
    {    
        foreach (Control control in this.Controls)            
        {
            if (control is Label && control.Name != "label4")
            {
                (control as Label).Forecolor = Color.Black;
            }
        }
    }
迷鸟归林 2024-12-17 17:07:37

在伪 C# 中:

List<String> controlsToChange =new List<String>{"lable1Name","lable2Name"};
foreach(Control control in form.Controls)
{
    if(control.GetType().Equals(typeof(Lable))
    {
         if( controlsToChange.Contains(control.Name)
         {
              control.Forecolor=Colour.Black;
         }
    }
}

in pseudo c#:

List<String> controlsToChange =new List<String>{"lable1Name","lable2Name"};
foreach(Control control in form.Controls)
{
    if(control.GetType().Equals(typeof(Lable))
    {
         if( controlsToChange.Contains(control.Name)
         {
              control.Forecolor=Colour.Black;
         }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文