Visual Studio 2022 Form Load not resetting controls

Gary Agoura 196 Reputation points
2022-02-06T22:00:33.673+00:00

I am using Visual Studio Community 2022 Version 17.0.5 with Net Framework 4.8.04161 and when I first start my program, and load a second form, everything works just fine. If I close the second form, then reload it again, a checkbox doesn't start out with the default method of "Checked - False" like it shows in the Properties window.

The first time I call the form (In the Form Load event), I set the checkbox checked to True. The CheckedChanged event fires and loads a grid. I do what I need to do, close the form. When I reload the form again, why doesn't the checkbox default to False? When the code is run again, I put a breakpoint just before the code that sets the checkbox to true and I noticed that it is already set to true and the event doesn't fire.

As a workaround, I put in the following code and it works, but I shouldn't have to do that.

checkbox.checked = false
checkbox.checked = true

Then it works like planned. Why does this happen?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,612 questions
{count} votes

Accepted answer
  1. Viorel 113.4K Reputation points
    2022-02-07T10:37:12.393+00:00

    Probably you are using notations like Form2.ShowDialog, Form2.Checkbox1, etc., which deals with the default instance of Form2 and keeps its properties. Sometimes this is convenient.

    To use new forms, replace Form2.ShowDialog with:

    Dim f As New Form2
    f.ShowDialog()
    

    This will always create a fresh form. But if this requires more changes, you can use your current workaround.

    0 comments No comments

0 additional answers

Sort by: Most helpful