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 Sub
What to do next
1. Add an IsInitializing property to the form:
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:
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.