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:
  1. ' Visual Basic 6.0
  2. Private Sub Check1_Click()
  3.    TextBox1.Text = "Check1 was clicked"
  4. End Sub
  5.  
  6. ' After upgrade to Visual Basic 2008
  7. ' UPGRADE_WARNING: Event CheckStateChanged may fire when form is initialized.
  8. Private Sub Check1_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Check1.CheckStateChanged
  9.    ' The following line will cause an error if TextBox1 is not
  10.    ' initialized.
  11.    TextBox1.Text = "Check1 was clicked"
  12. End Sub
What to do next 1. Add an IsInitializing property to the form:
  1. Private IsInitializing As Boolean
2. 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:
  1. Private Sub Check1_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Check1.CheckStateChanged
  2.    If Me.IsInitializing = True Then
  3.       Exit Sub
  4.    Else
  5.       TextBox1.Text = "Check1 was clicked"
  6.    End If
  7. 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

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

In reply to by Anonymous (not verified)

Hi, What do you mean? Where should we put the code to determine if the initialization is already finished?

In reply to by admin

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:
  1. Public Sub chkExist_Checked(...) handles chkExist.Checked
  2. If (Me.Created = true) Then
  3.    ' form has been loaded completely
  4. Else
  5.    ' form is still initialising
  6. End If
  7. End Sub

In reply to by admin

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