Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何限制Combobox添加重复值?_C# - Fatal编程技术网

C# 如何限制Combobox添加重复值?

C# 如何限制Combobox添加重复值?,c#,C#,我有一个类,用于向Combobox添加值(一个用于显示,另一个用于隐藏) 使用这个类,我向Combobox添加值 cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress, iConnectionID.ToString())); 但是我想用下面的方法限制重复的值 foreach (KeyValuePair<int, Object> ikey in m_lstConnectionID) {

我有一个类,用于向Combobox添加值(一个用于显示,另一个用于隐藏)

使用这个类,我向Combobox添加值

cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress, iConnectionID.ToString()));
但是我想用下面的方法限制重复的值

foreach (KeyValuePair<int, Object> ikey in m_lstConnectionID)
            {
                if (!cmbServerNo.Items.Contains(strIPAddress))
                {
                    cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress, iConnectionID.ToString()));
                }
            } 
foreach(MlstConnectionId中的KeyValuePair ikey)
{
如果(!cmbServerNo.Items.Contains(strIPAddress))
{
cmbServerNo.Items.Add(新的ComboBoxItem(strIPAddress,iConnectionID.ToString());
}
} 
但它可能同时添加了strIpAddress和ConnectionID,所以当我检查它是否包含时,它会失败。 如何解决这个问题?
谢谢

您可以使用LINQ的
任何扩展方法:

if (!cmbServerNo.Items.Any(x => x.ToString() == strIPAddress))
{
    cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress,
                                           iConnectionID.ToString()));
}

您可以使用LINQ的
任何
扩展方法:

if (!cmbServerNo.Items.Any(x => x.ToString() == strIPAddress))
{
    cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress,
                                           iConnectionID.ToString()));
}
您可以使用HashSet()

HashSet items=newhashset();
foreach(MlstConnectionId中的KeyValuePair ikey)
{
如果(!items.Contains(strIPAddress))
{
项目。添加(地址);
cmbServerNo.Items.Add(新的ComboBoxItem(strIPAddress,iConnectionID.ToString());
}
} 
您可以使用哈希集()

HashSet items=newhashset();
foreach(MlstConnectionId中的KeyValuePair ikey)
{
如果(!items.Contains(strIPAddress))
{
项目。添加(地址);
cmbServerNo.Items.Add(新的ComboBoxItem(strIPAddress,iConnectionID.ToString());
}
} 

如果要使用列表框或组合框中的
Items.Contains
函数,对象需要实现
IEquatable
接口

大概是这样的:

public class ComboBoxItem : IEquatable<ComboBoxItem> {
  // class stuff

  public bool Equals(ComboBoxItem other) {
    return (this.ToString() == other.ToString());
  }

  public override bool Equals(object obj) {
    if (obj == null)
      return base.Equals(obj);

    if (obj is ComboBoxItem)
      return this.Equals((ComboBoxItem)obj);
    else
      return false;
  }

  public override int GetHashCode() {
    return this.ToString().GetHashCode();
  }
}
// this should be added:
ComboBoxItem addItem = new ComboBoxItem("test", "test item");
if (!cb.Items.Contains(addItem)) {
  cb.Items.Add(addItem);
}

// this should not be added:
ComboBoxItem testItem = new ComboBoxItem("test", "duplicate item");
if (!cb.Items.Contains(testItem)) {
  cb.Items.Add(testItem);
}

如果要使用列表框或组合框中的
Items.Contains
函数,对象需要实现
IEquatable
接口

大概是这样的:

public class ComboBoxItem : IEquatable<ComboBoxItem> {
  // class stuff

  public bool Equals(ComboBoxItem other) {
    return (this.ToString() == other.ToString());
  }

  public override bool Equals(object obj) {
    if (obj == null)
      return base.Equals(obj);

    if (obj is ComboBoxItem)
      return this.Equals((ComboBoxItem)obj);
    else
      return false;
  }

  public override int GetHashCode() {
    return this.ToString().GetHashCode();
  }
}
// this should be added:
ComboBoxItem addItem = new ComboBoxItem("test", "test item");
if (!cb.Items.Contains(addItem)) {
  cb.Items.Add(addItem);
}

// this should not be added:
ComboBoxItem testItem = new ComboBoxItem("test", "duplicate item");
if (!cb.Items.Contains(testItem)) {
  cb.Items.Add(testItem);
}

您首先维护重复条目是有原因的吗?您首先维护重复条目是有原因的吗?@user662285:您可以在这种情况下使用非泛型。@user662285:您可以在这种情况下使用非泛型。