Change ComboBox.Text property in SelectedIndexChanged event

0.00 avg. rating (0% score) - 0 votes

It seems that you can’t change the Text property of a combobox inside SelectedIndexChanged event. The .Text will still contain the old value after the change due to cross-threading issues:

Workaround: Use delegation to do this, this will change the text right after the SelectedIndexChanged event:


this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke(new ChangeTextDelegete(ChangeText));
}

private delegate void ChangeTextDelegete();
public void ChangeText()
{
this.comboBox1.Text = “Changed”;
}


Problem: With this approach, sometimes “Exception has been thrown by the target of an invocation” fires randomly.


Reference:

How to change combobox text property after SelectedIndexChanged event?

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>