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());
}
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
Form 2
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:
In your
opennewform()
method you are instantiating a new copy ofForm2
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 originalForm2
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.