prevent control events from firing before form is loaded

B

BillE

I am working on a vb.net windows forms project using visual studio 2005.

Often on the form load event I populate combo boxes.

When a combo box is populated, the SelectedIndexChanged event fires,
sometimes multiple times.

Usually the SelectedIndexChanged event runs code which should only run when
it is a user selection, not the result of the combo box being populated.

What I have been doing is defining a boolean class variable for the form to
indicate if the form has loaded completely, and setting it to True when the
form is completely loaded, in the form activated event. On each
SelectedIndexChanged event I check to see if this boolean variable is true,
before executing code.

Is this the best way to prevent control events from firing before the form
is completely loaded, or if there is a more efficient way?

Thanks
Bill
 
L

Lloyd Sheen

BillE said:
I am working on a vb.net windows forms project using visual studio 2005.

Often on the form load event I populate combo boxes.

When a combo box is populated, the SelectedIndexChanged event fires,
sometimes multiple times.

Usually the SelectedIndexChanged event runs code which should only run
when it is a user selection, not the result of the combo box being
populated.

What I have been doing is defining a boolean class variable for the form
to indicate if the form has loaded completely, and setting it to True when
the form is completely loaded, in the form activated event. On each
SelectedIndexChanged event I check to see if this boolean variable is
true, before executing code.

Is this the best way to prevent control events from firing before the form
is completely loaded, or if there is a more efficient way?

Thanks
Bill

What I do is to not use the handles clause for those controls and use an
AddHandler after the control has it's data loaded. I use this also in
certain cases where I want to refresh the data but not change information on
a form about a previously selected item by using a:

RemoveHandler
PopulateControl
AddHandler

pattern.

Hope this helps
LS
 
S

Stephany Young

Delay the wiring up of the event handler.

Remove the 'Handles ...' from your ComboBox_SelectedIndexChanged event
handler and, instead,

AddHandler ComboBox.SelectedIndexChanged, New EventHandler(Addressof
ComboBox_SelectedIndexC]hanged)

as the last step in the Form.Load event handler, or, at least, after you
have finished initialising the ComboBox.
 

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