Bokambo
Bokambo

Reputation: 4480

How to restrict Combobox to Add Duplicate Values?

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

Upvotes: 1

Views: 3645

Answers (3)

LarsTech
LarsTech

Reputation: 81610

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

Upvotes: 0

Rich
Rich

Reputation: 3821

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

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

You could use LINQ's Any extension method:

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

Upvotes: 1

Related Questions