Form Load and CheckChange Events at times fire and not fire.

The code below will determine a user interaction with a checkbox. At times on the form load it will execute and other times it will not execute. This throws off my flag set variable at the start of the form load.

Any suggestions on how to handle (or a more efficient way) to solve my issue?

 Private Sub chkTPCalcActive_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTPCalcActive.CheckedChanged
        Dim GageConfiguration As String
        If TPFormLoaded = True Then
            TPFormLoaded = False
            Exit Sub
        End If
        If Me.chkTPCalcActive.Checked = True Then
            ' If TPCalcSwitchState = True Then
            ActivateTPCalculation = True
            TPCalcSwitchState = True
            GageConfiguration = ("<@:L" & GageID & "08<GAGE,P>")
            serialCOMDiscover.WriteLine(GageConfiguration)
            System.Threading.Thread.Sleep(1000)
            'ElseIf TPCalcSwitchState = True or  Then
            'ActivateTPCalculation = False
            'GageConfiguration = ("<@:L" & GageID & "08<GAGE,P>")
            'serialCOMDiscover.WriteLine(GageConfiguration)
            'System.Threading.Thread.Sleep(1000)
            ' End If
            'TPCalcSwitchState = True
        ElseIf Me.chkTPCalcActive.Checked = False Then
            ActivateTPCalculation = True
            TPCalcSwitchState = True
            GageConfiguration = ("<@:L" & GageID & "08<GAGE,P>")
            serialCOMDiscover.WriteLine(GageConfiguration)
            System.Threading.Thread.Sleep(1000)
        End If
    End Sub

Open in new window

cmdolcetAsked:
Who is Participating?
 
cmdolcetAuthor Commented:
I actually had to define a flag on the form entry/load and once the form was loaded I would change the flag state and this seems to work.
0
 
Robberbaron (robr)Commented:
in my experience, changing the checkbox from form load doesnt throw the event....

so you may want to move all the code into a separate (eg) chkTPCalcActions function, which is called from _CheckedChanged

in that way, you can set the checkbox on form load (from a previous settings or similar) and then call the chkTPActions to do other updates
0
 
cmdolcetAuthor Commented:
OK, but where do I put my function if I move it outside the event?
0
 
Robberbaron (robr)Commented:
same code file,
Private Sub chkTPCalcActive_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTPCalcActive.CheckedChanged

        chkTPCalcActions()
end Sub

Sub chkTPCalcActions()
        Dim GageConfiguration As String
        If TPFormLoaded = True Then
            TPFormLoaded = False
            Exit Sub
        End If
        If Me.chkTPCalcActive.Checked = True Then
            ' If TPCalcSwitchState = True Then
            ActivateTPCalculation = True
            TPCalcSwitchState = True
            GageConfiguration = ("<@:L" & GageID & "08<GAGE,P>")
            serialCOMDiscover.WriteLine(GageConfiguration)
            System.Threading.Thread.Sleep(1000)
            'ElseIf TPCalcSwitchState = True or  Then
            'ActivateTPCalculation = False
            'GageConfiguration = ("<@:L" & GageID & "08<GAGE,P>")
            'serialCOMDiscover.WriteLine(GageConfiguration)
            'System.Threading.Thread.Sleep(1000)
            ' End If
            'TPCalcSwitchState = True
        ElseIf Me.chkTPCalcActive.Checked = False Then
            ActivateTPCalculation = True
            TPCalcSwitchState = True
            GageConfiguration = ("<@:L" & GageID & "08<GAGE,P>")
            serialCOMDiscover.WriteLine(GageConfiguration)
            System.Threading.Thread.Sleep(1000)
        End If
    End Sub

Open in new window

0
 
cmdolcetAuthor Commented:
After taking a closer look on how to handle the event. I was able to set a flag on the form load event so if the event from the radio button was raised it would look to see if the form has been entered or not.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.