prevent control events from firing before form is loaded

  • Thread starter Thread starter BillE
  • Start date Start date
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
 
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
 
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.
 
Back
Top