Click here to Skip to main content
14,732,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a small C# application. I'm new in C#.

I have 2 forms form1 & form2 in that form1 is login form , after giving all credentials it redirect to form2. But form1 (login form)remain behind it. I have 2 closed it on successful login.
Please help me.
Thanks in advance.
Updated 27-Sep-20 5:02am
Abhinav S 11-Oct-10 13:04pm
Removed text speak,

Okay this is my first time awnsering to a post but i was looking for this awnser earlyer so i will try to help you

To show the 2nd form you would do
form2 f2 = new form2();

To hide for one simple do

form2 f2 = new Form2();

But the disadvantage to that is that, if you Close for2, Form 1 will still be hidden,
so if you want to have Form 2 Close you whole application when u close form2 do this

private void btnLogin_Click(object sender, EventArgs e)

                Form2 frm2 = new Form2();
                frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
        private void frm2_FormClosed(object sender, FormClosedEventArgs e)

i added that to my login button, so if they Succsesfuly login, it will hide Form1 and Show Form2, then when they want to close the application They just close Form2

i hope that made sense top you, i spent a long time figureing that out the other night

If you need anymore help let me know
OriginalGriff 11-Oct-10 10:33am
Not too bad - but you can make it simpler by using:
<pre> Form2 frm2 = new Form2();
If Form1 is your startup form (i.e. it comes up automatically when you start the application) then you cannot just close it and show Form2 as closing the form will terminate the application and close all forms.

1) Hide Form1 when you log in, and show Form2. You can then close either close Form1 (and the app) when Form2 closes, or re-display Form1 for a fresh log in.
2) Modify the Main method in Program.cs to take a result from Form1 (successful login or close application) and then display Form2 or close down as appropriate.
3) Make Form2 your startup form, and display your login form as part of the constructor.
Hello experts comment on this

you can do it without hiding form also

For that you have to use Main() in Program.cs as start up like this

public static void Main()

            UI.FormUserLogin objForm = new UI.FormUserLogin();
            if (objForm.ShowDialog() == DialogResult.OK)
                Application.Run(new UI.FormMain(objForm.UserInfo));

here your Login form must return whether user is authenticate or not.

please let me know if you have any problem to understand it.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900