Search code examples
vb.netvisual-studio-2010formscheckboxvb.net-2010

Checkbox events when Form is opened and closed fire when form is reloaded


First... I am open to skinning this cat a different way if I am going at it wrong to begin with. Using VB 2010 .net 4.0 and I am very much a beginner.

I am making a product billing application that has a main form and a subform with additional options. Whenever that subform is reopened after being opened once, the checkbox events that were selected are blank by default. If I recheck them (so someone can uncheck) then any that are rechecked all refire and increase the variable again.

I ultimately need to be able to open that second form after closing it, display any checkboxes that were selected before as selected again and not increase the variable in the process.

Main form Checkbox code to set booleans and increase or decrease subtotal variable of most used products.

    Private Sub chkbox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox1.CheckedChanged
    If chkbox1.Checked = True Then
        bChkbox1 = True
        Subtotal += 15
    Else
        bChkbox1 = False
        Subtotal -= 15
    End If
End Sub

Main form button to launch subform with all products listed.

    Private Sub btnAllProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAllProducts.Click
    Form3.Show()
End Sub

Subform checkbox code works perfectly the first time it is opened but not when relaunched.

Private Sub chkbox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox2.CheckedChanged
    If chkbox2.Checked = True Then 'also tried without the nested if with same results
        If Me.IsHandleCreated = True Then 'me.visible behaves the same way
            MsgBox("form visible true")'launches after clicking button but before form is actually on screen
            Form1.bcheckbox2 = True
            Form1.Subtotal += 105
        End If
    Else
        Form1.bcheckbox2 = False
        Form1.Subtotal -= 105
    End If
End Sub

Booleans are used to check boxes that were checked on the main page or when it was open before.

    If Form1.bcheckbox2 = True Then
        chkbox2.Checked = True
    End If

As I said, I can completely rework the code if it makes sense to do so or just fix something if I have made some sort of mistake.

For example, I was thinking of changing to wipe the subtotal on each form load and rebuild it based off the toggled booleans but it seems like there should be a much more elegant way with less overhead and I am just doing something incorrectly.


Solution

  • It is not common to have to tell checks and radios to ignore events while loading the form. You just need an Ignore or Loaded flag:

    Public Class Form1
       Private ignore As Boolean = True
       ...
    
     Private Sub Form1_Load(...
        ' do normal stuff
    
        ignore = False               ' should be the ONLY place it is set
     End Sub
    
    Private Sub CheckBox2_CheckedChanged(...
       If ignore Then Exit Sub
    
    End Sub
    

    The Form Designer code will fire events as it creates the form and controls, which CAN be handy for initializing stuff but often it causes trouble. Some controls will even get the same event twice. There isnt really a "reload" action for forms. If you hide them, Show() won't fire the Load event again.

    You can avoid the flag and manually add the handlers for the troublesome controls when the form loads, but that can be tedious if there are lots of them. Flags can be abused and misused, but if it is set in that one spot only, its fine.