[ACCEPTED]-How do I Change comboBox.Text inside a comboBox.SelectedIndexChanged event?-selectedindexchanged

Accepted answer
Score: 16

This code should work...

public Form1()

    comboBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3" });

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    String text = "You selected: " + comboBox1.Text;

    BeginInvoke(new Action(() => comboBox1.Text = text));

Hope it helps... :)


Score: 2

You should reset the SelectedIndex property to -1 when 1 setting the Text property.

Score: 2

Move your change code outside of combobox 1 event:

if(some condition)
    BeginInvoke(new Action(() => comboBox.Text = "new string"));
Score: 1

Perhaps it would help if you could explain 10 exactly what you're trying to do. I find 9 that the SelectionChangeCommitted event 8 is considerably more useful for purposes 7 like what you describe than SelectedIndexChanged. Among 6 other things, it's possible to change the 5 selected index again from SelectionChangeCommitted 4 (e.g. if the user's selection is invalid). Also, changing 3 the index from code fires SelectedIndexChanged 2 again, whereas SelectionChangeCommitted 1 is only fired in response to user actions.

Score: 1

In short, .NET is trying to prevent an endless 11 loop that could occur. When a change to 10 the Text property occurs, .NET will try 9 to match that new value to the current items 8 and change the index for you, thereby firing 7 the SelectedIndexChanged event again.

People 6 coming here looking for a VB implementation 5 of Delegates can refer to the code below

'Declares a delegate sub that takes no parameters
Delegate Sub ComboDelegate()

'Loads form and controls
Private Sub LoadForm(sender As System.Object, e As System.EventArgs) _
    Handles MyBase.Load
    ComboBox1.Items.Add("This is okay")
    ComboBox1.Items.Add("This is NOT okay")
End Sub

'Handles Selected Index Changed Event for combo Box
Private Sub ComboBoxSelectionChanged(sender As System.Object, e As System.EventArgs) _
    Handles ComboBox1.SelectedIndexChanged
    'if option 2 selected, reset control back to original
    If ComboBox1.SelectedIndex = 1 Then
        BeginInvoke(New ComboDelegate(AddressOf ResetComboBox))
    End If

End Sub

'Exits out of ComboBox selection and displays prompt text 
Private Sub ResetComboBox()
    With ComboBox1
        .SelectedIndex = -1
        .Text = "Select an option"
    End With
End Sub

Further Reading: See 4 this post (mine) on changing Combobox Text in the SelectedIndexChanged Event which goes into a little 3 more detail as to why you need to use a 2 delegate as a workaround to change the ComboBox 1 Text.

Score: 1

//100% worked

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
      BeginInvoke(new Action(() => ComboBox1.Text = "Cool!");


Score: 0

A ComboBox will bind to whatever object 14 collection you specify, as opposed to simply 13 having a text/value combination that you 12 find in DropDownLists.

What you'll need to 11 do is go into the ComboBox's Items collection, find 10 the item you want to update, update whatever 9 property you have being bound to the Text 8 field in the ComboBox itself and then the 7 databinding should automatically refresh 6 itself with the new item.

However, I'm not 5 100% sure you actually want to modify the 4 underlying data object being bound, so you 3 may want to create a HashTable or some other 2 collection as a reference to bind to your 1 ComboBox instead.

Score: 0

you should use:

BeginInvoke(new Action((text) => comboBox.Text 1 = text), "new text to set");

Score: 0

I searching for solution for same issue. But 1 solve it by handling Format event:

cbWatchPath.Format += new System.Windows.Forms.ListControlConvertEventHandler(this.cbWatchPath_Format);

private void cbWatchPath_Format(object sender, ListControlConvertEventArgs e)
        e.Value = "Your text here";

More Related questions