Hi,

I like to add DisplayMember and ValueMember to combobox when Loading forms. Then with combobox_SelectedIndexChanged event select the DisplayMember but assign ValueMember to a textbox field. I can add and assign the DisplayMember, but I cannot do it with ValueMember. in Designer.cs part at initiation;

this.cmbViziteType.Items.AddRange(new object[] {
            "desease",
            "medication",
            "control"});

in the form

private void cmbViziteType_SelectedIndexChanged(object sender, EventArgs e)
        {
                    dfViziteType.Text = cmbViziteType.Text;
        }

This works fine. But what I want is;
"desease","malaria",
"medication","aspirin",
"control","ktrl"});
first column being display member and secon valuemember. On SelectedIndexChanged I like to assign ValueMembers to the textbox when I select displaymember.
So how can I form Displaymember and valuemember pair properly and secondly how can I make the selection as described
I hope I made myself clear.
Thanks in advance
snky

Is this what you are looking for:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                // if your item is text, otherwise cast to proper type:
                string text = (string)comboBox1.SelectedItem;
                switch (text)
                {
                    case "disease":
                        textBox1.Text = "malaria";
                        break;
                    case "medication":
                        textBox1.Text = "aspirin";
                        break;
                    case "control":
                        textBox1.Text = "ktrl";
                        break;
                    default:
                        break;

                }
            }

You may wish to consider doing it this way:

Dictionary<string, string> dict = new Dictionary<string,string>();

            public void AddItems()
            {
                dict.Add("desease", "malaria");
                dict.Add("medication", "aspirin");
                dict.Add("control", "ktrl");

                foreach (KeyValuePair<string, string> kv in dict)
                {
                    comboBox1.Items.Add(kv.Key);
                }
            }
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string key = comboBox1.SelectedText;
                textBox1.Text = dict[key]; // get the value of associated key...
            }

Edited 7 Years Ago by DdoubleD: Additional

Both approaches look nice. I tried the second one. This is what I did:

private void cmbViziteType_Clicked(object sender, EventArgs e)
        {
            AddItemsToCombo();
        }
    
       public void AddItemsToCombo()  
       {    
            dict.Add("desease", "malaria");
            dict.Add("medication", "aspirin");
            dict.Add("control", "ktrl");

            foreach (KeyValuePair<string, string> kv in dict)
            {
                cmbViziteType.Items.Add(kv.Key);
            }
        
       }

//Above lines work fine....................

 private void cmbViziteType_SelectedIndexChanged(object sender, EventArgs e)
            {
                string key = cmbViziteType.SelectedText;
                dfViziteType.Text = dict[key]; // get the value of associated key...
            }
//here I get the following exception. dict[key] has null value
//System.Collections.Generic.KeyNotFoundException was unhandled

Am I missing something ?

Thanks,
snky

Sorry, wrong property. Change to:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string key = (string)comboBox1.SelectedItem;
            textBox1.Text = dict[key];
        }

Great !
it works. I will give a try your first suggestions as well as soon as possible.
Thanks again
snky

This question has already been answered. Start a new discussion instead.