Save checkbox states in a dynamic just for the current process [closed]

I’ve made a form with a single button that opens a dynamic form which has 3 checkboxes so what I want to do is save the controls states after so that they’re the same when you close and reopen the dynamic form this is the dynamic form

Dynamic Form

I want the form to reopen with the controls in the same state after closing, just for this process. I don’t want it to reopen like that the next time I start the actual application up again here’s the code I have so far

namespace SaveState
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form frm = new Form();
            Label lbl = new Label();
            CheckBox chckBox = new CheckBox();
            CheckBox chckBox2 = new CheckBox();
            CheckBox chckBox3 = new CheckBox();

            lbl.Text = "Hello";
            lbl.AutoSize = true;
            lbl.Location = new Point(53, 42);
            lbl.Font = new Font("Calibri", 9.25F, FontStyle.Bold);
            lbl.ForeColor = Color.Black;

            chckBox.Location = new Point(53, 72);
            chckBox.Text = "How Are You";
            chckBox.AutoSize = true;

            chckBox2.Location = new Point(53, 94);
            chckBox2.Text = "How Have You Been";
            chckBox2.AutoSize = true;

            chckBox3.Location = new Point(53, 120);
            chckBox3.Text = "See Ya";
            chckBox3.AutoSize = true;

            frm.Size = new Size(465, 407);
            frm.Controls.Add(lbl);
            frm.Controls.Add(chckBox);
            frm.Controls.Add(chckBox2);
            frm.Controls.Add(chckBox3);

            frm.ShowDialog();
            frm.FormClosing += delegate
            {

            };
        }
    }

Answer

You put your Form frm as a class level variable and do all its setup in the constructor, not the button click

You tweak the formclosing event so that it hides the form instead of closing it:

frm.FormClosing += (object sender, EventArgs e) => {  e.Cancel = true; (sender as Form).Hide();  };

The only thing your button should do is ShowDialog the frm

Leave a Reply

Your email address will not be published. Required fields are marked *