Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7919529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:53:49+00:00 2026-06-03T15:53:49+00:00

I have A Class which is used to add Values to Combobox(One is used

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:53:51+00:00Added an answer on June 3, 2026 at 3:53 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file attachment class (FileAttachment) which is used in several other classes.
I have class which have one public method Start , one private method and
I have a singleton class which is being used throughout the app. I am
I have a class which contains a static collection that can be used across
I have a library which has a basic class which is used extensively by
Let's say I have a list in a class which will be used in
I have a silly, little class FileSystemSize which can be used both as an
I have class Money which is an @Embeddable @Embeddable public class Money implements Serializable,
Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which
I have a structure that I used to populate Class Structures with values: MyType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.