Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In a program I have 2 forms. First one is used to get "Username" and "Password" from the user. If the entered information is correct, the second form is shown. Now I want to close the first form or at least hide it not to be seen so I used the code below but it did not close or hide the first form. Could you please let me know how could I make it work?

Thank you a lot

private void button1_Click(object sender, EventArgs e)
        {
            string InputUserName = "Ali";
            string InputPassword = "4862597k";

            string strUserName = textBox1.Text;
            string strPassword = maskedTextBox1.Text;

            if (strPassword == InputPassword && strUserName == InputUserName )
            {
                Form2 f2 = new Form2();
                f2.Show();
                Form1 f1 = new Form1();
                f1.Hide();  // f1.Close();
            }
            else
            {
                MessageBox.Show("Wrong Password or Username, Try again.",
                                "Alert", MessageBoxButtons.OK, 
                                MessageBoxIcon.Warning);

                maskedTextBox1.Text = "";
            }
        }
Posted

Well, when you do this:
Form2 f2 = new Form2();
f2.Show();
Form1 f1 = new Form1();
f1.Hide();  // f1.Close();

you're creating two new forms, showing one and hiding the other. Don't you want to hide the form that's already shown instead of hiding a new one that you just created? I think you meant this.Hide() instead of making a new Form1.

Also, see this[^] answer that I posted to a very similar question. It describes in vague terms a better solution for creating and showing a login form.
 
Share this answer
 
Comments
Dalek Dave 23-Jul-10 9:25am    
Good answer.
ahhashemi 23-Jul-10 9:34am    
Reason for my vote of 5
Directly aimed my need
you should replace this with ur code



if (strPassword == InputPassword && strUserName == InputUserName
{
Form2 f2 = new Form2();
f2.Show();



this.Hide(); ///////


}

just try it .
 
Share this answer
 
Comments
ahhashemi 23-Jul-10 23:19pm    
Reason for my vote of 5
Found it helpfull
For the approach you're taking, I would likely suggest adding a public method called CloseForm() on the form you want to close "remotely". This will allow you to do other shutdown items if required down the road, logic that should be on that class and not this one.

When you create an instance of Form1 to show the username/password dialog, keep a reference to it. Use the reference to invoke the CloseForm() method (rather than creating a new one as you do above).

Cheers.
 
Share this answer
 
v3
Comments
ahhashemi 23-Jul-10 9:32am    
Reason for my vote of 3
It assumed that i am a advanced level programmer understanding his hints easily. Although I didn't take my answer from this reply because of lacking explaining code, I thank him for his fast reply.

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