Keep checked radio button checked through forms C#

2019-09-08 15:02发布

I have a form with 10 radio buttons (Form2)for a user to check (all in same group). Then a button to go to the next form (Form3).

On Form3 I have a back button to go back to Form2 to change the radio button if needed.

When the back button is pressed, it goes to Form2 with all of the radio buttons, but it doesn't show the previously checked radio button.

Example code:

string SchoolName = "";

if (radioButton1.Checked)
{
    SchoolName = radioButton1.Text;
}

if (radioButton2.Checked)
{
    SchoolName = radioButton2.Text;
}

and then going back to the previous form using back button:

private void button3_Click(object sender, EventArgs e)
{         
    this.Close();

    th = new Thread(opennewform);
    th.SetApartmentState(ApartmentState.STA);
    th.Start();
}

private void opennewform(object obj)
{
    Application.Run(new Form2());
}

标签: c# forms
3条回答
闹够了就滚
2楼-- · 2019-09-08 15:18

You are not going back to the same instance of the form, you are creating a new form. See my two form project below

Form 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form2.Show();
            string  results = form2.GetData();
        }
    }
}

Form 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 nform1)
        {
            InitializeComponent();

            this.FormClosing +=  new FormClosingEventHandler(Form2_FormClosing);
            form1 = nform1;
            form1.Hide();
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //stops form from closing
            e.Cancel = true;
            this.Hide();
        }
        public string GetData()
        {
            return "The quick brown fox jumped over the lazy dog";
        }

    }
}
查看更多
聊天终结者
3楼-- · 2019-09-08 15:24

recreating the form like new Form2() will add a initialized form with default values, and that will result in loose of your changes.

to solve you can:

  • play with show/hide instead of close/new
  • save object state, when re-opening form2 for example you can call a constructor like


    new form2(check1State,check2State,selectedDropItem,txtName...);


查看更多
兄弟一词,经得起流年.
4楼-- · 2019-09-08 15:27

In your opennewform() method you are instantiating a new copy of Form2 and not going back to the one you came from originally. That's why your original radio button selection is not being saved. You need to somehow return to the original Form2 instance instead of creating a new instance.

For example, you can hide the Form2 when user closes it and re-Show it when user needs it again.

查看更多
登录 后发表回答