簡體   English   中英

如何在winform上單擊(紅色X)按鈕右上角退出應用程序

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

我的申請表中有兩種表格。 frmLoginfrmDash 登錄后。 我點擊登錄按鈕hiding frmLogin 顯示frmDash廣告。

frmDash ,有LogOut按鈕。 點擊this.Close() ,我正在使用this.Close()並顯示登錄表單。 但現在,如果我點擊(red X)按鈕frmLogin整個應用程序沒有終止。 請給出一些建議。 我試過這個:

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


        }

frmDash Logout按鈕的frmDash

 private void button1_Click(object sender, EventArgs e)

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

        }

您在登錄並隱藏表單時創建frmDash的新實例。 然后當你注銷時,你會說this.close()並創建另一個新的FrmLogin實例。 不要回到FrmLogin的原始實例。

這意味着您將始終擁有您開始使用的隱藏實例。 (如果關閉FrmLogin的新實例,隱藏的FrmLogin仍然存在。)

您可以在btnLogin_Click中添加以下內容:

frmDash.ParentForm = this;

和button1_Click應如下所示:

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

如果您實現此目的,您將顯示初始登錄表單,當您關閉它時,您將關閉登錄表單的初始實例。

@Edit 10:52 25-06-2015無法分配ParentForm並且只讀。 解決方案是將其分配給Parent,或者以下內容也可以應用於btnLogin_Click:

frmDash.Owner = this;

和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(下一個問題)

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


    }

使用Application.Exit(); 單擊按鈕時的方法

如果您使用自定義按鈕,請執行此操作

    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
    }

如果您使用的是X按鈕,請添加FormClosing事件,代碼如下所示:

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
}

將其添加到Designer.cs

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

然后右鍵單擊SampleClassName_FormClosed並單擊“轉到定義”。

然后在創建的類中調用要關閉的表單。

在您的2.表格上嘗試以下內容:

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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM