Stop SelectedIndexChanged from Firing on Form Load Event

Submitted by: 
Visitors have accessed this post 11455 times.

If you are using SelectedIndexChanged in your ComboBox may be you encounter a problem with events being fired even if you did not click the ComboBox.

This happens when you open and close your Windows Form. The workaround for this is to use RemoveHandler statement.

Here’s an example:

  1. RemoveHandler CreditTermIDComboBox.SelectedIndexChanged, AddressOf CreditTermIDComboBox_SelectedIndexChanged

And when you’re done executing the Form Load event you have to add back the handler again by using the AddHandler statement.

Here’s an example:

  1. AddHandler CreditTermIDComboBox.SelectedIndexChanged, AddressOf CreditTermIDComboBox_SelectedIndexChanged

The above example is good a solution but there is a better one for this by using the SelectionChangeCommitted event.

SelectionChangeCommitted event occurs when the selected item has changed and that change is displayed in the ComboBox.

By using SelectionChangeCommitted event you do not need to declare RemoveHandler and AddHandler statement.

Note: SelectedIndexChanged event is fired only if it is bound to a DataSet.


Comments

I am quite new to the .net environment (about 4 months now) (programmer for 25 years) and it never occured to me to remove an event handler for control created events. This solves a load of my issues when loading forms, so a big thankyou for the post. This is one to remember and is a great example of thinking outside of the box. IR, Grmsby, UK.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.