1

我想在将该项目添加到组合框之前检查我的数组中的项目是否出现在我的组合框中,以避免重复

我不允许使用 LINQ

代码:

private void ToonCategorien()
    {
        cboCategorie.Items.Clear();
        foreach (String sCategorie in marrCategorie){
            if (!cboCategorie.Items.Contains(sCategorie))
            {
                ComboBoxItem cboItem = new ComboBoxItem();
                cboItem.Content = sCategorie;
                cboCategorie.Items.Add(cboItem);
            }
        }
    }

抱歉在我的 C# 代码中使用了荷兰语。

所以marrCategorie是一个数组,其中包含我从 StreamReader 读取的所有类别。问题是他无论如何都会添加所有内容。我认为这是我的 if 循环中的一个问题。

我也试过if(cboCategorie.Text.Contains(sCategorie))没有结果。

我还不允许直接发布图片,很抱歉使用超链接):

结果

提前致谢!

解决方案,感谢博卢:

    private void ToonCategorien()
    {
        cboCategorie.Items.Clear();
        foreach (String sCategorie in marrCategorie){
            if (!cboCategorie.Items.Contains(sCategorie))
            {
                cboCategorie.Items.Add(sCategorie);
            }
        }
    }
4

1 回答 1

2

您正在stringComboBoxItem此处进行比较,我认为您可以只使用字符串:例如:

private void ToonCategorien()
    {
        cboCategorie.Items.Clear();
        foreach (String sCategorie in marrCategorie){
            if (!cboCategorie.Items.Contains(sCategorie))
            {                
                cboCategorie.Items.Add(sCategorie);
            }
        }
    }
于 2013-07-15T15:53:17.350 回答