1

I have A Class which is used to add Values to Combobox(One is used for display and other as Hidden)

public class ComboBoxItem
    {
        string displayValue;
        string hiddenValue;

    //Constructor
    public ComboBoxItem(string displayVal, string hiddenVal)
    {
        displayValue = displayVal;
        hiddenValue = hiddenVal;
    }

    //Accessor
    public string HiddenValue
    {
        get
        {
            return hiddenValue;
        }            
    }
    //Override ToString method
    public override string ToString()
    {
        return displayValue;
    } 

Using this class i add Values to Combobox

cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress, iConnectionID.ToString()));

But i want to restrict duplicate values i am using the below approach

foreach (KeyValuePair<int, Object> ikey in m_lstConnectionID)
            {
                if (!cmbServerNo.Items.Contains(strIPAddress))
                {
                    cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress, iConnectionID.ToString()));
                }
            } 

But it Guess it Adds both strIpAddress and ConnectionID so when i check it contains it fails. How to resolve this problem ? Thanks

Bokambo
  • 4,204
  • 27
  • 79
  • 130

3 Answers3

1

You could use LINQ's Any extension method:

if (!cmbServerNo.Items.Any(x => x.ToString() == strIPAddress))
{
    cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress,
                                           iConnectionID.ToString()));
}
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
0

You could use a HashSet (MSDN)

HashSet<String> items = new HashSet<String>();
foreach (KeyValuePair<int, Object> ikey in m_lstConnectionID)
            {
                if (!items.Contains(strIPAddress))
                {
                    items.Add(strIPAddress);
                    cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress, iConnectionID.ToString()));
                }
            } 
Rich
  • 3,781
  • 5
  • 34
  • 56
  • @user662285: You can use the non-generic [`Hashtable`](http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx) in that case. – Daniel Hilgarth May 09 '12 at 13:13
0

If you want to use the Items.Contains function from a ListBox or ComboBox, the object needs to implement the IEquatable interface.

Something like this:

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();
  }
}

Now the Items.Contains should work like this:

// 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);
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225