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 HobbsPosted Aug 2, 2011, 4:47 PM
if (radioButton1.Checked)
{
f = new Form2();
f.ShowDialog();
}
else
if (radioButton2.Checked)
{
f = new Form3();
f.ShowDialog();
}
else
MessageBox.Show("Nothing selected");
Zoran HorvatPosted Aug 4, 2011, 3:54 AM
Please don't forget to tick "This is correct answer" in post which has helped you solve the problem.
Thanks,
Zoran
NitsPosted Aug 4, 2011, 1:10 AM
Sam HobbsPosted Aug 3, 2011, 3:31 PM
Zoran HorvatPosted Aug 3, 2011, 4:22 AM
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
NitsPosted Aug 3, 2011, 4:04 AM
Sam HobbsPosted Aug 3, 2011, 2:27 AM
Do you understand the difference between showing a form using the Show method and showing a form using the ShowDialog method?
NitsPosted Aug 3, 2011, 12:27 AM
Sam HobbsPosted Aug 2, 2011, 4:37 PM
VulpesPosted Aug 2, 2011, 4:24 PM
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.
Zoran HorvatPosted Aug 2, 2011, 3:33 PM
This code is opening the form selected by radio buttons.
Zoran
NitsPosted Aug 2, 2011, 3:30 PM
Zoran HorvatPosted Aug 2, 2011, 12:55 PM
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