0
votes

I have a combobox as below with 3 items ("abc", "abc" & "bbb"). The combobox also has AutoCompleteMode "SuggestAppend" & AutoCompleteSource "ListItems" properties. Now I want the text box to show the SelectedIndex of the combobox as below:

http://i.imgur.com/MJ4JdDN.png

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.textBox1.Text = this.comboBox1.SelectedIndex.ToString(); 
    }

Everything seems fine until I select the 2nd "abc", the SelectedIndexChanged event will enter for the 1st time & display the index properly on the text box. But when the combobox lost focus, the SelectedIndexChanged event will fire again, causing the index to be displayed wrongly. I found that it only happens to items with the same value. Is there a way I can stop the event from firing twice?

http://i.imgur.com/gEw46xf.png

            this.comboBox1.DataSource = dt;
            this.comboBox1.DisplayMember = "Description"; //Some descriptive field to be shown in combobox
            this.comboBox1.ValueMember = "Code"; //Unique code that user won't understand
            this.comboBox1.SelectedIndex = -1;
1
why having a combobox that contains identical values?Mivaweb
try e.Handled=true in your selectionchange methodVishal
it's in win form... where can i find my xaml code? :p SelectedIndexChanged doesn't seem to have e.Handled KeyPressArgs?.... I need to have identical values because the uniqueness of the combobox is in "ValueMember" rather than the "DisplayMember"Wai Chuen Ho
try using SelectionChangeCommitted it should only fire onceNocturnal
SelectionChangeCommitted isn't really suitable because the "DropDownStyle" of the ComboBox is "DropDown", so the index will not change if users type the value in the boxWai Chuen Ho

1 Answers

1
votes

put comboBox1_SelectedIndexChanged code in comboBox1_ValueChanged and there u'll get index in event arguments(sender i.e your comboBox cast it in combobox).

use comboBox1.SelectedIndex and u'll get the index.