[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4072
  • Last Modified:

Give a ComboBox a Key/Value Pair

I'm working in .NET 1.1, and trying to figure out how to assign combox items a key/value pair.
in the combobox, i need to display a list of options with a key behind each value displayed.  

For Example:
"Apple" - 0
"Orange" - 3
"Grapes" - 5
i tried something like cboGrocery.Items.Add(new DictionaryEntry("Apple", "0")), but the combobox will only shows the object Type in the drop down (i.e. "System.Collections.DictionaryEntry").

i understand the combo box of .NET 1.1 doesn't comes with the key/value features, so i'm looking for a simple custom solutions.  Please keep in mind that i'm developing in .NET 1.1 C#.

Thanks.
0
asenna25
Asked:
asenna25
  • 4
4 Solutions
 
multithreadingCommented:
Make your own class which contains your two parts (key & value). Override "ToString" to return the part you want displayed in the combobox. Add these objects to your combobox. Cast them back when you index into them and get your key.
0
 
JimBrandleyCommented:
In .Net 1.1, the Combobox.Items collection can take objects. The text that shows up on the screen comes from invoking each item's ToString() method. So, one way to accomplish what you want is to build a simple class.
public class CBItem
{
   public string Name = string.Empty;
   public int Key = 0;
   public CBItem( string name, int key )
   {
      Name = name;
      Key = key;
   }
   public override ToString()
   {
      return Name;
   }
}

Now you can display the name, and you can get the Key value from the selected item.

Jim
0
 
JimBrandleyCommented:
multithreading - Didn't mean to step on you. For some reason, it showed no responses yet when I opened it.

Jim
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
asenna25Author Commented:
this is what i wrote:
public class ComboItemData
{
      private string m_Value;
      private string m_DisplayText;
      public ComboItemData(string strVal, string strText)
      {
            m_Value = strVal;
            m_DisplayText = strText;
      }
      public override string ToString()
      {
            return m_Value;
      }
}
then in the application:

cboGrocery.Items.Add(new ComboItemData("yummy", "Apple")); //<== Combox shows "yummy", not "Apple"
string strX = cboCurrency.Items[cboCurrency.SelectedIndex].ToString(); //<== strX shows "yummy"

what did i do wrong?

Thanks.
0
 
JimBrandleyCommented:
ToString() needs to return what uou want displayed. So change to this:
return m_DisplayText;

Jim
0
 
JimBrandleyCommented:
Also, after an entry is selected, you need to get the value from that object, so you will either need to make the value member public, or add a public property.

Jim
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now