Home All Groups Group Topic Archive Search About

prevent control events from firing before form is loaded

Author
15 Jun 2009 12:28 PM
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

Author
15 Jun 2009 2:35 PM
Lloyd Sheen
Show quote Hide quote
"BillE" <bel***@datamti.com> wrote in message
news:O1BdFTb7JHA.2456@TK2MSFTNGP02.phx.gbl...
>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
Author
15 Jun 2009 2:39 PM
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.


Show quoteHide quote
"BillE" <bel***@datamti.com> wrote in message
news:O1BdFTb7JHA.2456@TK2MSFTNGP02.phx.gbl...
>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
>
Author
15 Jun 2009 2:47 PM
BillE
Thanks!
Bill
Show quoteHide quote
"BillE" <bel***@datamti.com> wrote in message
news:O1BdFTb7JHA.2456@TK2MSFTNGP02.phx.gbl...
>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
>