You are on page 1of 4

11/14/2014

c# - How to open a new form from another form - Stack Overflow


sign up

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.

log in

tour

help

stack overflow careers

Take the 2-minute tour

How to open a new form from another form


I have form which is opened using ShowDialog Method. In this form i have a Button called More. If we click
on More it should open another form and it should close the current form.
on More Button's Click event Handler i have written the following code
MoreActions objUI = new MoreActions ();
objUI.ShowDialog();
this.Close();
But what is happening is, it's not closing the first form. So, i modified this code to
MoreActions objUI = new MoreActions ();
objUI.Show();
this.Close();
Here, The second form is getting displayed and within seconds both the forms getting closed.
Can anybody please help me to fix issue. What i need to do is, If we click on More Button, it should open
another form and close the first form.
Any kind of help will be really helpful to me.
c#

.net

winforms

.net-2.0

asked Oct 19 '10 at 3:03


Dinesh
684 3 16 43
killer app..... yonan2236 Oct 19 '10 at 3:27
I think you need Wizard see stackoverflow.com/questions/1613232/ Saeed Amiri Oct 19 '10 at 8:06
add a comment

5 Answers
In my opinion the main form should be responsible for opening both child form. Here is some pseudo that
explains what I would do:
// MainForm
private ChildForm childForm;
private MoreForm moreForm;
ButtonThatOpenTheFirstChildForm_Click()
{
childForm = CreateTheChildForm();
childForm.MoreClick += More_Click;
childForm.Show();
}
More_Click()
{
childForm.Close();
moreForm = new MoreForm();
moreForm.Show();
}
You will just need to create a simple event MoreClick in the first child. The main benefit of this approach is
that you can replicate it as needed and you can very easily model some sort of basic workflow.

http://stackoverflow.com/questions/3965043/how-to-open-a-new-form-from-another-form

1/4

11/14/2014

c# - How to open a new form from another form - Stack Overflow


answered Oct 19 '10 at 5:38
Johann Blais
5,837 4 21 45
In winforms the use of events to manipulate the GUI is the basic of all "smart" stuff. Events are definitly the way
to go here +1 Neowizard Oct 19 '10 at 9:24
add a comment

If I got you right, are you trying like this?

into this?

in your Form1, add this event in your button:


// button event in your Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(); // Shows Form2
}
then, in your Form2 add also this event in your button:
// button event in your Form2
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3(); // Instantiate a Form3 object.
f3.Show(); // Show Form3 and
this.Close(); // closes the Form2 instance.
}
answered Oct 19 '10 at 3:59
yonan2236
3,690 11 46 98

Why do you use f2.ShowDialog(); in the first case and f3.Show() in the second case? superpuccio Feb 21 at
9:22

add a comment

I would use a value that gets set when more button get pushed closed the first dialog and then have the
original form test the value and then display the the there dialog.
For the Ex

http://stackoverflow.com/questions/3965043/how-to-open-a-new-form-from-another-form

2/4

11/14/2014

c# - How to open a new form from another form - Stack Overflow


1.
2.
3.
4.

Create three windows froms


Form1 Form2 Form3
Add One button to Form1
Add Two buttons to form2

Form 1 Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool DrawText = false;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
if (f2.ShowMoreActions)
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
}

Form2 code
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public bool ShowMoreActions = false;
private void button1_Click(object sender, EventArgs e)
{
ShowMoreActions = true;
this.Close();
}

private void button2_Click(object sender, EventArgs e)


{
this.Close();
}
}

Leave form3 as is
answered Oct 19 '10 at 3:19
rerun
15.6k 2 23 58
add a comment

ok so I used this:
public partial class Form1 : Form
{
private void Button_Click(object sender, EventArgs e)
{
Form2 myForm = new Form2();
this.Hide();
myForm.ShowDialog();
this.Close();
}
}
This seems to be working fine but the first form is just hidden and it can still generate events. the
"this.Close()" is needed to close the first form but if you still want your form to run (and not act like a
launcher) you MUST replace it with

http://stackoverflow.com/questions/3965043/how-to-open-a-new-form-from-another-form

3/4

11/14/2014

c# - How to open a new form from another form - Stack Overflow


this.Show();
Best of luck!
edited Jul 15 '13 at 20:40
FKunecke
880 2 16

answered Jul 15 '13 at 20:17


user2584967
41 2

This question already has an answer selected, and it was last active over 6 months ago. You may want to
consider leaving a comment in this situation instead of posting another answer. FKunecke Jul 15 '13 at 20:39
add a comment

Try this..
//button1 will be clicked to open a new form
private void button1_Click(object sender, EventArgs e)
{
this.Visible = false;
// this = is the current form
SignUp s = new SignUp(); //SignUp is the name of my other form
s.Visible = true;
}
edited Oct 1 '12 at 5:45
Arun Singh
841 2 9 32

answered Oct 1 '12 at 5:26


fmp
1

Isn't SignUp.Show() neccesary ? Gonzalo.- Oct 2 '12 at 14:11


add a comment

Not the answer you're looking for? Browse other questions tagged c# .net winforms
.net-2.0 or ask your own question.

http://stackoverflow.com/questions/3965043/how-to-open-a-new-form-from-another-form

4/4

You might also like