如何在关闭按钮的同时打开主窗体关闭事件中的登录表单? [英] How to open login form in closing event of main form while click in close button ?

查看:84
本文介绍了如何在关闭按钮的同时打开主窗体关闭事件中的登录表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Program.cs中:

 静态  void  Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
登录login = new 登录();
Application.Run(登录);
if (login.LoginSuccess == true
{
Application.Run( new Form_Main());

}
}



成功登录后,它会打开主窗体( Form_Main .cs)。现在我想在关闭Form_Main后再次将 FormClosing 事件添加到Run Login 表单。



In my Form_Main.cs:

  public  Form_Main()
{
InitializeComponent();
this .FormClosing + = new System.Windows.Forms.FormClosingEventHandler( .Form_Main_FormClosing);
}
私有 void Form_Main_FormClosing( object sender,FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show( 你真的想关闭这个应用吗? 结束活动! ,MessageBoxButtons.YesNo);
if (dr == DialogResult.No)

e.Cancel = true ;


else
{
e.Cancel = ;
Application.Run( new Login());

}
}



这不起作用。实际上在这里我想关闭名为Form_Main的表单,并希望显示另一个名为Login的表单。请帮我在FormClosing活动中做到这一点。



提前致谢。



我尝试了什么:



如何在关闭按钮的同时打开主窗体关闭事件中的登录表单?

解决方案

不要。

当Main表单关闭时,Main方法中的Application.Run将返回。此时,您可以循环再次执行登录代码。但是......确保你提供一些方法(除了任务管理器)让用户终止整个应用程序,否则他们会在你回家并关掉电脑的时候非常恼火...

In my Program.cs :

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Login login = new Login();
            Application.Run(login);
            if (login.LoginSuccess == true)
            {
                Application.Run(new Form_Main());
                
            }
        }


After successfully login it opens main form (Form_Main.cs). Now I want to add FormClosing event to Run Login form again after closing Form_Main.

In my Form_Main.cs :

public Form_Main()
        {
            InitializeComponent();
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_Main_FormClosing);
        }
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("Do you REALLY want to close this app?", "Closing event!", MessageBoxButtons.YesNo);
            if (dr == DialogResult.No)
            
                e.Cancel = true;
            

            else
            {
                e.Cancel = false;
                Application.Run(new Login());            
                  
            }
        }


This is not working. Actually in here I want to close form named Form_Main and want to show another form named Login. Please, help me in FormClosing event to do that.

Thanks in advance.

What I have tried:

How to open Login Form in Closing event of Main Form while click in close button ?

解决方案

Don't.
When the Main form closes, the Application.Run in your Main method will return. At that point you can loop round to do your login code again. But...make sure you provide some method (other than Task Manager) to let the user terminate the whole application, or they will get very annoyed with you when it's time to go home and turn the PC off...


这篇关于如何在关闭按钮的同时打开主窗体关闭事件中的登录表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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