Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an c# Winform application in Visual studio ...
when my project runs Login form is opened for user that take user name and password
when user provide correct information this login form closed and Main MDI open which suddenly closed itself after a second
i want to Prevent them from auto closing
so Please can any one knows how to fix???
C#
public void login()
        {
            string a = "No";
            if (txtuser.Text != "" & txtpass.Text != "")
            {
             string queryText = "SELECT Count(*) FROM tbllogin " +
                         "WHERE username = @Username AND pass = @Password";
                using (SqlConnection cn = new SqlConnection("Server= localhost;                
database=tailoringmng; integrated Security=true"))
                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    cn.Open();
                    cmd.Parameters.AddWithValue("@Username", txtuser.Text);  
                    cmd.Parameters.AddWithValue("@Password", txtpass.Text);
                    int result = (int)cmd.ExecuteScalar();
                    if (result > 0)
                    {
                        frmmainmdi mdi = new frmmainmdi();
                        a = "Yes";
                        mdi.A = "Yes";
                        mdi.Show();



                        this.Close();
                    }
                    else
                        MessageBox.Show("User Not Found!");
                }

            }
            

        }
Posted
Updated 20-Mar-14 10:53am
v2
Comments
Sergey Alexandrovich Kryukov 20-Mar-14 16:35pm    
What is "mdi"? Do you really use MDI? Don't...
—SA

It seems that Login() is your main form, that is why when it is closed, other are automatically closed.. So instead what you can do is, set the visibility of the form the to false.
C#
public string login()
        {
            string a = "No";
            if (txtuser.Text != "" & txtpass.Text != "")
            {
             string queryText = "SELECT Count(*) FROM tbllogin " +
                         "WHERE username = @Username AND pass = @Password";
                using (SqlConnection cn = new SqlConnection("Server= localhost;
database=tailoringmng; integrated Security=true"))
                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    cn.Open();
                    cmd.Parameters.AddWithValue("@Username", txtuser.Text);
                    cmd.Parameters.AddWithValue("@Password", txtpass.Text);
                    int result = (int)cmd.ExecuteScalar();
                    if (result > 0)
                    {
                        frmmainmdi mdi = new frmmainmdi();
                        a = "Yes";
                        mdi.A = "Yes";
                        mdi.Show();

                        this.Visible = false;
                    }
                    else
                        MessageBox.Show("User Not Found!");
                }

            }


        }

-KR
 
Share this answer
 
Comments
abdul manan 7 20-Mar-14 16:22pm    
yes my main form is login form but when i click logout from menu string and again log in it open multiple mdi one already open second again open by login function
abdul manan 7 20-Mar-14 16:31pm    
Second when i close all mdi the hide one still open
is there any way to close hide forms
Sergey Alexandrovich Kryukov 20-Mar-14 16:34pm    
This is quite possible, but how?
What happens if the user clicks close button or hits Alt+F4? Don't you think you have to provide a solution for that? Otherwise, your advice won't work...
—SA
Krunal Rohit 20-Mar-14 16:36pm    
same goes for the Form_Closing() as well...

-KR
Sergey Alexandrovich Kryukov 20-Mar-14 16:41pm    
The mere mention of the name "Form_Closing" does not tell anything. There is not an event, "Form_Closing", right? :-)
—SA
You can prevent closing main form if you handle the event FormClosing. This event is cancellable. You can cancel closing on some condition, if you assign true to the property Handled of the event arguments parameter passing. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventhandler(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs(v=vs.110).aspx[^].

When you cancel the closing, you can call Hide instead.

There are differen techniques. You can close the application and start it again, in your Main<code> method (entry point). Say, you can run <code>Application.Run in cycle (or several times), with different form passing as a parameter. Sometimes it is used to give the application two main form, say, one is loging or splash, and, on second run, "real" main. Only remember: you cannot re-use truly closed form; it is disposed.

—SA
 
Share this answer
 
v2
Comments
abdul manan 7 20-Mar-14 17:09pm    
make it simple
when frmlogin close //this is may main form
the mdi stay open this is my other form which i want to make secondary main form after frmlogin
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new frmmainmdi());
Application.Run(new frmLogon());
Sergey Alexandrovich Kryukov 20-Mar-14 21:03pm    
When you say "mdi", what do you mean? MDI child? parent?
By "second main form" I mean one at a time. Application.Run exists, and starts again with different form...
—SA
abdul manan 7 21-Mar-14 5:15am    
MDI parent

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