Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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();

        }
like image 365
Dinav Ahire Avatar asked Jun 25 '15 07:06

Dinav Ahire


2 Answers

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();
        }


    }
like image 59
Revils Avatar answered Sep 26 '22 15:09

Revils


If you are using a custom button, do this

    private void QuitButton_Click(object sender, EventArgs e)
    {
        DialogResult DialogResult = MessageBox.Show("Do you really want to exit?", "Confirmation",
            MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
        if (DialogResult == DialogResult.Yes)
            Application.Exit();//Here is the code to close everything
        else
            //Do stuff
    }

If you are using the X button, add a FormClosing Event and the code look like this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e){
    DialogResult DialogResult = MessageBox.Show("Do you really want to exit?", "Confirmation",
            MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
        if (DialogResult == DialogResult.Yes)
            Application.Exit();//Here is the code to close everything
        else
            //Do stuff
}
like image 43
Sven Borden Avatar answered Sep 24 '22 15:09

Sven Borden