How to Prevent Code from Executing its Event when Form is Still Initializing
When I try to convert hotel reservation system from visual basic 6.0 to visual basic.net 2008 I found a lot of error in the even log of VB.NET.
One of the important logs I found is on how to prevent code from executing its event when forms is still initializing.
The following is an information from visual studio 2008 documentation:
In Visual Basic 6.0, events for a form or control were not raised until the form was finished loading. In Visual Basic 2008, the InitializeComponent method handles the initialization of the form and its controls; the order in which objects are initialized cannot be modified. In some cases, this may cause events to be raised during initialization. If the event handler references other components that have not yet been initialized, this could cause a run-time error.
For example, if the Click event handler for a CheckBox control sets a value in a TextBox control, an error may occur if the TextBox control is not yet initialized:
' Visual Basic 6.0 Private Sub Check1_Click() TextBox1.Text = "Check1 was clicked" End Sub ' After upgrade to Visual Basic 2008 ' UPGRADE_WARNING: Event CheckStateChanged may fire when form is initialized. Private Sub Check1_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Check1.CheckStateChanged ' The following line will cause an error if TextBox1 is not ' initialized. TextBox1.Text = "Check1 was clicked" End SubWhat to do next
1. Add an IsInitializing property to the form:
Private IsInitializing As Boolean2. Set the property to true in the form's constructor just prior to the InitializeComponent call; set it to false immediately following the call.
3. Add the following logic to the event procedure to check the value of the property before running the code:
Private Sub Check1_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Check1.CheckStateChanged If Me.IsInitializing = True Then Exit Sub Else TextBox1.Text = "Check1 was clicked" End If End Sub
I want to share to you this kind issue because I found question on the internet on where to put the "IsInitializing" code.
In my case using my hotel reservation system (vb.net version) I put it under the Form Load event. Both the IsInitializing = True and IsInitializing = False.
Please check the form Check In in my hotel reservation system.

Comments
It is more easy!!
Hi, I want to tell you that it is more easy than you think, in VS2008 the win form had "created" property that let you know if the form is fully created.
When form is initializing 'created' is false and when finish become true.
I hope it help you.
Rodrigo Lobo. Costa Rica
re: It is more easy!!
Hi,
What do you mean? Where should we put the code to determine if the initialization is already finished?
re: It is more easy!!
I guess, you need to check the property of the forms for True or False in the event handler of your control.
Remember, he mentioned that .net framework 3+ handles the setting of the Created property for you.
for example:
we don't need to determine if
we don't need to determine if initialization is finished
when the event "Created" fires up
it already means that the initialization has finished
code goes under it
Add new comment