如何从上(红色的X)按钮,点击应用退出右上角上的winform [英] how to exit from application on click of (red X ) button right top on winform

查看:728
本文介绍了如何从上(红色的X)按钮,点击应用退出右上角上的winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序两种形式。 frmLogin frmDash 。登录后。我是隐藏 frmLogin 上登录按钮的点击。广告展示 frmDash



frmDash ,还有注销按钮。在注销的点击,我使用 this.Close()并显示登录表单。但现在如果我点击(红色的X) frmLogin 整个应用程序的按钮不会终止。 PLZ给一些建议。
I已经试过这样:

 私人无效btnLogin_Click(对象发件人,EventArgs五)
{

{
this.Hide();

串Log_API =http://api.retailbutton.co/WS/Service.php?Service=employeeLogin;
如果(LoginUser(Log_API))
{
logIn_Status =真;
GlolbalUtil.LogIn_Status = logIn_Status;
frmDash frmDash =新frmDash();
frmDash.Owner =这一点;
frmDash.Show();
txtUsername.Text =;
txtPassword.Text =;
//GlolbalUtil.accept_status =1;
}
,否则
{
MessageBox.Show(请检查的用户名和密码);
FrmLogin frmLogin =新FrmLogin();
frmLogin.Owner =这一点;
frmLogin.Show();
}


}

有关退出代码

 私人无效的button1_Click(对象发件人,EventArgs的: frmDash 按钮)

{
GlolbalUtil.LogIn_Status =假;
this.Close();
FrmLogin FL =新FrmLogin();
fl.Show();

}


解决方案

您创建frmDash的新实例,当你登录并隐藏窗体。
然后当你退出,你说this.close(),并创建FrmLogin的另一个新的实例。不会回到FrmLogin的原始实例。



这意味着你将永远有你开始使用隐藏的实例。
(如果关闭FrmLogin的新实例,隐藏FrmLogin仍然存在)



您可以添加以下的btnLogin_Click:

  frmDash.ParentForm =这一点; 

和的button1_Click应该是这样的:

 私人无效的button1_Click(对象发件人,EventArgs五){
GlolbalUtil.LogIn_Status =假;
FrmLogin FL =(FrmLogin)this.Parent; //在此之前该公司表示ParentForm
this.Close();
fl.Show();
}

如果您实现这一点,你会显示在初始登录表单,并当您关闭它,您关闭登录表单的初始实例。



@Edit 10:52 25-06-2015
ParentForm不能被指定且为只读。一个解决方案是将其分配给家长或以下也可应用于btnLogin_Click:

  frmDash.Owner =这一点; 

和的button1_Click:

 私人无效的button1_Click(对象发件人,EventArgs五){
GlolbalUtil.LogIn_Status =假;
FrmLogin FL =(FrmLogin)this.Owner
this.Close();
fl.Show();
}



@Edit 08:16 29-06-2015(下一个问题)

 私人无效btnLogin_Click(对象发件人,EventArgs五)
{

{


串Log_API =http://api.retailbutton.co/WS/Service.php?Service=employeeLogin;
如果(LoginUser(Log_API))
{
logIn_Status =真;
GlolbalUtil.LogIn_Status = logIn_Status;
frmDash frmDash =新frmDash();
frmDash.Owner =这一点;

////如果隐藏在这里,你不必做
//一个新的实例时,如果语句不是true.////
本。隐藏();

frmDash.Show();
txtUsername.Text =;
txtPassword.Text =;
//GlolbalUtil.accept_status =1;
}
,否则
{
MessageBox.Show(请检查的用户名和密码);

////删除以下////
// FrmLogin frmLogin =新FrmLogin();
//frmLogin.Owner =这一点;
//frmLogin.Show();
}


}


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


    }

这篇关于如何从上(红色的X)按钮,点击应用退出右上角上的winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆