I have created a simple form on which I have given two radio buttons & 1 button. So by selecting either of the radio buttons I want to open another two forms respectively on button click.
I have tried but it opens only 1 form by selecting any of the radio button, So any help????
Loading

Sam HobbsRank #107 · 20.5K reputationPosted Aug 2, 2011, 12:47 PM EDT
if (radioButton1.Checked)
{
f = new Form2();
f.ShowDialog();
}
else
if (radioButton2.Checked)
{
f = new Form3();
f.ShowDialog();
}
else
MessageBox.Show("Nothing selected");
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 2, 2011, 8:55 AM EDT
Form frm1 = new Form();
frm1.Text = "One";
Form frm2 = new Form();
frm2.Text = "Two";
Form frm = null;
if (radioButton1.Checked)
frm = frm1;
else if (radioButton2.Checked)
frm = frm2;
if (frm != null)
frm.ShowDialog();
Zoran
Nits37 reputationPosted Aug 2, 2011, 11:30 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 2, 2011, 11:33 AM EDT
This code is opening the form selected by radio buttons.
Zoran
VulpesRank #55 · 29.2K reputationPosted Aug 2, 2011, 12:24 PM EDT
Alternatively, if you just want a blank form, except the title, create an instance of the base Form class - as Zoran did in his code - and show that.
Sam HobbsRank #107 · 20.5K reputationPosted Aug 2, 2011, 12:37 PM EDT
Nits37 reputationPosted Aug 2, 2011, 8:27 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Aug 2, 2011, 10:27 PM EDT
Do you understand the difference between showing a form using the Show method and showing a form using the ShowDialog method?
Nits37 reputationPosted Aug 3, 2011, 12:04 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 3, 2011, 12:22 AM EDT
private void btnPopulate_Click(object sender, EventArgs e)
{
Form frm = null;
if (radioButton1.Checked)
{
frm = new Form2();
}
else if (radioButton2.Checked)
{
frm = new Form3();
}
if (frm != null)
{
frm.Owner = this;
frm.FormClosed += new FormClosedEventHandler(ChildFormClosed);
this.Hide();
frm.ShowDialog();
}
}
void ChildFormClosed(object sender, FormClosedEventArgs e)
{
Form frm = (Form)sender;
frm.Owner.Show();
}
Note that this piece of code handles FormClosed event on child form to show current form back once the child is closed.
Zoran
Sam HobbsRank #107 · 20.5K reputationPosted Aug 3, 2011, 11:31 AM EDT
Nits37 reputationPosted Aug 3, 2011, 9:10 PM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 3, 2011, 11:54 PM EDT
Please don't forget to tick "This is correct answer" in post which has helped you solve the problem.
Thanks,
Zoran