【问题标题】:How can I search in Label[] for item.Text? [duplicate]如何在 Label[] 中搜索 item.Text? [复制]
【发布时间】:2019-07-10 05:04:04
【问题描述】:

我是编程新手,所以我想这个问题有点愚蠢。我想检查LabelsArray 中是否存在当前文本,因为有时我会两次获得相同的名称。

for (int i = 0; i < DM.checkedListBox1.CheckedItems.Count; i++)
{
    labels[i] = new Label();
    for (int j = i; j < DM.dataGridView1.Columns.Count; j++)
    {
        if (DM.dataGridView1.Columns[j].Visible)
        {                        
            if (labels.Contains(DM.dataGridView1.Columns[j].HeaderText))
            {
                labels[i].Text = DM.dataGridView1.Columns[j].HeaderText;
                break;
            }
        }
    }
}

谢谢!

【问题讨论】:

  • 你可以使用bool exists = labels.Any(l =&gt; l.Text == searchText);

标签: c# winforms


【解决方案1】:

如果您想知道标签数组是否包含特定文本,您需要搜索它们的Text 属性:

labels.Select(l => l.text).Contains(DM.dataGridView1.Columns[j].HeaderText)

我使用Select LINQ 扩展方法将Labels 的数组更改为strings 的数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-15
    • 2020-03-06
    • 2016-10-04
    • 1970-01-01
    • 2012-09-25
    • 2015-09-30
    • 2018-05-19
    • 1970-01-01
    相关资源
    最近更新 更多