Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Opening child form makes that the UI controls get reset

I am a beginner at windows forms, and I am having some issues with opening child forms.

My application has 2 buttons, one that goes to the main menu, and another that goes to the options.

The problem is that, if I click on a checkbox in the options menu and leave the options tab and come back, the checkbox will not be checked anymore.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is my code:

private Form CurrentChildForm;

private void OpenChildForm(Form childForm)
{
  if(CurrentChildForm != null)
  {
    CurrentChildForm.Visible = false;
  }
  CurrentChildForm = childForm;
  childForm.TopLevel = false;
  childForm.FormBorderStyle = FormBorderStyle.None;
  childForm.Dock = DockStyle.Fill;
  PanelForForm.Controls.Add(childForm);
  PanelForForm.Tag = childForm;
  childForm.BringToFront();
  childForm.Show();
}

private void MainMenu_Click(object sender, EventArgs e)
{
  OpenChildForm(new MenuForm());
}
private void OptionsMenu_Click(object sender, EventArgs e)
{
  OpenChildForm(new OptionsForm());
}

>Solution :

through your Click-Events on the different buttons you are always creating a new instance of your Forms.

A possible solution is to cache the instance of your optionsMenu for example through a private field, because I consider it being SingleInstance.

private Form CurrentChildForm;
private OptionsForm _opForm;

private void OptionsMenu_Click(object sender, EventArgs e)
{
    if (_opForm == null)
    {
        _opForm = new OptionsForm();
    }

    OpenChildForm(_opForm);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading