Dinav Ahire Dinav Ahire - 3 months ago 8
C# Question

how to exit from application on click of (red X ) button right top on winform

i have two forms in my application.

frmLogin
and
frmDash
. after login. i am
hiding
frmLogin
on click of login button. ad showing
frmDash
.

in
frmDash
, there is
LogOut
button. on click of LogOut, i am using
this.Close()
and showing login form. but now if i click
(red X)
button of
frmLogin
whole application is not terminating. plz give some suggestions.
i have tried this.:

private void btnLogin_Click(object sender, EventArgs e)
{
try
{
this.Hide();

string Log_API = "http://api.retailbutton.co/WS/Service.php?Service=employeeLogin";
if (LoginUser(Log_API))
{
logIn_Status = "true";
GlolbalUtil.LogIn_Status = logIn_Status;
frmDash frmDash = new frmDash();
frmDash.Owner = this;
frmDash.Show();
txtUsername.Text = "";
txtPassword.Text = "";
//GlolbalUtil.accept_status = "1";
}
else
{
MessageBox.Show("Please Check Username and password");
FrmLogin frmLogin = new FrmLogin();
frmLogin.Owner = this;
frmLogin.Show();
}


}


code for Logout button of
frmDash
:

private void button1_Click(object sender, EventArgs e)

{
GlolbalUtil.LogIn_Status = "false";
this.Close();
FrmLogin fl = new FrmLogin();
fl.Show();

}

Answer

You create a new instance of frmDash when you log in and hide the form. Then when you are logging out, you say this.close() and create another new instance of FrmLogin. Not going back to the original instance of FrmLogin.

This means that you will always will have the hidden instance which you started with. (If you close the new instance of FrmLogin, the hidden FrmLogin still exists.)

You can add the following in btnLogin_Click:

frmDash.ParentForm = this;

and button1_Click should look like this:

private void button1_Click(object sender, EventArgs e){
    GlolbalUtil.LogIn_Status = "false";
    FrmLogin fl = (FrmLogin)this.Parent;  //Prior it said ParentForm
    this.Close();
    fl.Show();
}

If you implement this, you will show the initial login form and when you close it, you close the initial instance of the login form.

@Edit 10:52 25-06-2015 ParentForm cannot be assigned and is read only. A solution is to assign it to Parent or the following can also be applied in btnLogin_Click:

frmDash.Owner = this;

and button1_Click:

private void button1_Click(object sender, EventArgs e){
    GlolbalUtil.LogIn_Status = "false";
    FrmLogin fl = (FrmLogin)this.Owner
    this.Close();
    fl.Show();
}

@Edit 08:16 29-06-2015 (Next question)

private void btnLogin_Click(object sender, EventArgs e)
{
    try
    {


        string Log_API = "http://api.retailbutton.co/WS/Service.php?Service=employeeLogin";
        if (LoginUser(Log_API))
        {
            logIn_Status = "true";
            GlolbalUtil.LogIn_Status = logIn_Status;
            frmDash frmDash = new frmDash();
            frmDash.Owner = this;

            ////If you hide here, you do not have to make 
            //a new instance when the if statement is not true.////
            this.Hide();

            frmDash.Show();
            txtUsername.Text = "";
            txtPassword.Text = "";
            //GlolbalUtil.accept_status = "1";
        }
        else
        {
            MessageBox.Show("Please Check Username and password");

            ////Delete following////
            //FrmLogin frmLogin = new FrmLogin();
            //frmLogin.Owner = this;
            //frmLogin.Show();
        }


    }
Comments