使用this.hide时如何关闭应用程序 [英] how to close application when this.hide is used

查看:190
本文介绍了使用this.hide时如何关闭应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式form1和form2
在form1中,我给出了一个按钮单击事件.显示第二种形式.

i have two forms form1 and form2
in form1 i have given a button click event. which shows the second form.
by

form2 f2 = new form2();
this.hide;
f2.show();



在第二种形式中,我使用了这样的形式关闭事件



and in second form i have used a form closing event like this

public void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {

            if (MessageBox.Show("do you want to save changes to your text?", "My application", MessageBoxButtons.YesNo) == DialogResult.No)
            {

                e.Cancel = true;

            }

            else
            {

                e.Cancel = false;
            }


但我必须手动停止调试.
如何通过单击默认表单关闭按钮{X button}


but i manually have to stop debugging.
how to stop debugging by clickign the default form close button{X button}

推荐答案

来停止调试,您可以使用:
Application.Exit();
you may use :
Application.Exit();


您所显示的代码片段显示,不幸的是,您对如何进行编程了解甚少.请注意此错误,请不要重复:您的if-else块完全是多余的.

您需要具有以下内容:
The code fragment you show reveals that, unfortunately, you have very little understanding on how to do programming. Note this mistake and don''t repeat it: your if-else block is totally redundant.

You need to have something like this:
//check "updated" status you always need to have, because this is something important
if (!updated) return; // no need to ask confirmation if no data was updated in UI
e.Cancel = (MessageBox(/* ... */) == DialogResult.No); //that's it: Boolean is assigned to Boolean



这不会回答您的问题,但这是因为不必回答您的问题.上面显示的方法可以正常工作;并且它应该与调试器下的完全一样(只是因为否则您将不知道要调试的内容).您始终可以通过IDE停止调试,这是唯一正确的选择,就是您不想保存文件或继续执行其他任何操作.

—SA



This won''t answer your question, but this is because your answer does not have to be answered. The approach shown above works correctly; and it should work exactly like that under debugger (just because otherwise you would not know what are you debugging). You can always stop debugging via the IDE, which is the only right thing is you don''t want to save file or continue execution with anything else.

—SA


如果要在关闭第二个表单后显示隐藏的表单,可以将此方法添加到第一个表单中:

If you want to show the hidden form after closing the second form you can add this method to first form:

public void ShowForm<T>() where T : BaseForm, new()
{
    T form = new T();
 
   form.FormClosed += delegate(object sender, FormClosedEventArgs e)
   {
       this.Show();
   };

   this.Hide();
   form.Show();
}



并在bottun_CLick中调用它;



And call it in bottun_CLick;

public void bottun_Click(object sender, EventArgs e)
{
    ShowForm<form2>();
}


这篇关于使用this.hide时如何关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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