简体   繁体   中英

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

        }

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


    }

use Application.Exit(); method when your button clicked

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
}

Add this to your Designer.cs

this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.SampleClassName_FormClosed);

then right click the SampleClassName_FormClosed and click "Go to Definition".

then inside the created class call the form that you want to close.

Try the following on your 2. Form:

public partial class Form2 : Form
{
    private bool ForceClose = true;

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (ForceClose)
            Application.Exit();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        GlolbalUtil.LogIn_Status = "false";
        ForceClose = false;
        this.Close();
    }
}
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {

         Application.Exit();//close the whole program using x (red)button
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM