I have a form that instantiates and opens another form. It was working fine until recently, while re-purposing the second form that the click event on a Radio Button on the form has started being fired after form load. I have code in the form.shown event but it is never called (the form crashes during the radio button click event, as it is not supposed to be called so soon). The call stack window suggests that a PictureBox click event is also being fired right before or after the RadioButton click event.
Form Load:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblCountdown.TextAlign = ContentAlignment.TopCenter
lblCountdown.Font = New Font(lblCountdown.Font.Name, lblCountdown.Font.Size + 10, lblCountdown.Font.Style)
radFirstRunContinue.Checked = False
radFirstRunContinue.Visible = False
For Each picBox As PictureBox In tlpButtonTableLayout.Controls
AddHandler picBox.Click, AddressOf Me.PictureBox_click
Next
End Sub
Sometimes form controls will fire when the form is initialized. This might sound like a bug, but Microsoft calls it "by design".
Check and make sure only one instance of the form is being loaded. If you access a variable or control in the form from outside, it will initiate an implicit form load. To check this, just set a breakpoint at the beginning of the Load event handler.
You can use a flag (initialized to true, for example) that control handlers can use to see the form is being loaded. You can reset the flag in the form Shown event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With