SelectedIndexChanged of ComboBox control

C

Confach

I have defined SelectedIndexChanged event of ComboBox in a form.
I open this form ,and it will fire Form_Load event, which binds the
data t o comboBox.In this time,it also fire SelectedIndexChanged
events.
My question: How to avoid calling SelectedIndexChanged event when
Form Load event is fired?
 
T

tamberg

If you implement the handler yourself you can use a boolean (e.g.
updating) to prevent the handler from doing something:

bool updating; // default init value = false

// ...

updating = true;
try {
// update ComboBox items
} finally {
updating = false;
}

// ...

void SelectedIndexChanged (object source, EventArgs e) {
if (!updating) {
// handle index change
}
}
 
J

Joseph Byrns

You can also just add the event handler to the combobox after the data has
been bound.
i.e.

private void Form_Load(object sender, EventArgs e)
{
DataBindControls();
this.comboBox1.SelectedIndexChanged += new
System.EventHandler(comboBox1_SelectedIndexChanged);
}

or VB

Priavte Sub Form_Load(ByVal Sender as Object, ByVal e as EventArgs)
DataBindControls()
AddHandler me.comboBox1.SelectedIndexChanged, AddressOf
comboBox1_SelectedIndexChanged
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top