Visual Studio 2022 Form Load not resetting controls

Gary Agoura 206 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?

Developer technologies | VB
{count} votes

Accepted answer
  1. Viorel 122.6K 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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.