[英]Check if value appears in combobox
我想检查数组中的某个项目是否在将其添加到组合框之前出现在组合框中,以避免重复 。
我不允许使用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)) ,但没有结果。
我还不能直接发布图片,非常抱歉使用超链接):
提前致谢!
解决方案,感谢Bolu:
private void ToonCategorien()
{
cboCategorie.Items.Clear();
foreach (String sCategorie in marrCategorie){
if (!cboCategorie.Items.Contains(sCategorie))
{
cboCategorie.Items.Add(sCategorie);
}
}
}
您在这里将string
与ComboBoxItem
进行比较,我想您可以只使用字符串:例如:
private void ToonCategorien()
{
cboCategorie.Items.Clear();
foreach (String sCategorie in marrCategorie){
if (!cboCategorie.Items.Contains(sCategorie))
{
cboCategorie.Items.Add(sCategorie);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.