I have a application with two forms: Form1 and Form2 now Form1 displays Form2, but how can I go back to Form1, I know it will not help if I create another instance of Form1.

What I have

private void button1_Click(object sender, EventArgs e)
    Form2 FormMain = new Form2();


private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    Form1 Form1 = new Form1();

But as I said it does not work. I hate Forms...anyway that I can do this?

Nonsense! Forms are your friend! Here's how you can get it to work, provided, that Form2 acts as a dialog - that is, you display Form2 wait for the user to exit the box, then close Form2 and collect the information from now hidden form.

In Form1 (in the button1_Click method):

Form2 f = new Form2();
f.ShowDialog();  // Code will hang here, waiting for Form2 to go away
f.Close();// Discard the form

In Form2, put a single, lone text box, called "textBox1", and a button, called "button1", then:

private void button1_Click(object sender, EventArgs e)
    this.Hide(); // Hides the form, but it is still alive!

public string getText()
    return textBox1.Text;

The public "getText" function is called from Form1 after the user presses the button in Form2. The code in Form1 is able to "reach into" Form2 to call the public function and get the contents of the text box. Then, you call "Close" on the instance of Form2 to get rid of it.

when you are showing form2, hide form1.
And when you want to go back to form1 from form2, close (or hide form2) and show form1.
As simple as that.

Yes, Mitja, it is as simple as that!

Thanks, just one more thing I can't seem to get working.

Form2 is logIn page and form1 the main form of the application. How can terminate the application from Form2?


Is not working.

I have a while loop running in "public Form1()" to display form2 until the user logs In correctly or Clicks the close button.


while (!Form2.LogIn)
    if (Form2.CloseForm)


private void buttonClose_Click(object sender, EventArgs e)
    CloseForm = true;

Any ideas, its the 1st time I'm working with Forms, so please forgive me:)

What exactly is your Form2? A login?

if so you better use a ShowDialog method. This is an example:

using (Form2 form2 = new Form2())
                    form2.StartPosition = FormStartPosition.CenterScreen;
                    if (form2.ShowDialog() == DialogResult.OK)
                        //if login is ok, form1 can start
                        Application.Run(new Form1());
                    //else, application will close or exit by it self.

Yes Form2 is a LogIn window...Form1 is the main window(can change if needed)

Look what I need to is Open LogIn Form then if the user has Logged in successfully it need to display the Next form. Then I need a close button on the LogIn window to terminate the application(Or if the user click the "x", that I can do). But I also need to provide a function to "Log Out" that will take the user back to LogIn form.

What will be the best way of doing this?

Note to James(You know who): I sound like a noob, damn James I missing you...Why did you leave me to do the damn forms thing!

lol... think a bit . it wont hurt you. Use your brains, take a pen into your hand and write down some examples, how does it all has to go - openings forms, closing them ans so on.
YOu need to start using your own brains, we can do all the work instead of you - we can help you, but you are "requesting" from us to do all the job - where`s the point then?

If you dont want to use the pen and a paper to do some basic sketches, then you can think of what you need:
- Form1 is the MAIN form.
- Login is the additional form, but has to start before form1.
- Login windows HAS TO close by it self if the login is succeeded
- if Login is NOT correct login has to stay open and give a user another chance of inserting userName and pswd.
- Your login form can has a close button (the same as X button on the top right), which has to close the loginForm and Form1 (main form).
- for to logout, you need to create a list of yousers (you can usea generic List)
but it has to be on form1, or someplase else, BUT has nothing to do with login form.
- You can decide if the user has to enter his password to logout or not (to prevent that some one else will logout the user - if the application has more then one user at thre time logged it on the same pc).

THIS are the facts - and this is how you have to start coding. Not just run here and ask us for the code (poor James...)

Are you willing to help us here Singlem, willing to cooperate?
If so, I can help you out either.
And no hard feelings please, otheriwse you will never learn coding.


WoW...I do have a pen a paper that is next to me with all that I need to do, most of it is done. I created the main form and did all the coding already, I added the logIn form and did all the coding(connecting to database and validation). I have all the logic of the application worked out.

I have the LogIn starting before the Main form...All the coding is done, but not working as it should. I'm not getting the application to close if the user hits the close button("x" of the LogIn form)

I'm using showDialog to display the LogIn and that is working as it should. All I want to know how can I close the application. My current code closes the main form but does not terminate the application.

Look I understand that if one is just given all the code one will never learn anything, but I hit a deadlock with my coding. I'm not "requesting" anything, all that I'm looking for is how can get this damn app to work as it should. I'm new to forms in C#, give me a console app and I can make it dance for you.

If you still have problem (even if I gave you the example how this suppose to be done), you can send me your project to me. I will try to repair it, so it will work like it should.

Thanks for the offer, but I have to apologize...that example worked, stupid me just put all the calls in the wrong place. Thanks for the help

Sometimes you guys just need to be a bit slower and read the code we gave you a bit more precise. Anyway, I am glad you have salved the issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.