Avatar of cmdolcet
cmdolcet
Flag for United States of America asked on

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

Visual Basic.NET

Avatar of undefined
Last Comment
cmdolcet

8/22/2022 - Mon
Robberbaron (robr)

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
cmdolcet

ASKER
OK, but where do I put my function if I move it outside the event?
Robberbaron (robr)

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
cmdolcet

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
cmdolcet

ASKER
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.