简体   繁体   English

在对话框关闭或执行下一个操作之前,我必须单击多次……我该如何解决这个问题?

[英]I have to click multiple times before the dialog box closes, or performs the next action…How do I fix this?

Here is my C# code:这是我的 C# 代码:

private void StudentReg_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult dialog = MessageBox.Show("Do you really want to close this window?", "Exit", MessageBoxButtons.YesNo);

    if (dialog == DialogResult.Yes) 
    {
        Application.Exit();
    }
    else if (dialog == DialogResult.No)
    {
        e.Cancel = true;
    }
}

I doubt if you really want Application.Exit();我怀疑你是否真的想要Application.Exit(); : you prompt (bold is mine) :你提示(粗体是我的)

Do you really want to close this window你真的要关闭这个window

It is the window ( Form ), not the entire Aplication you want to close.它是window ( Form ),而不是您要关闭的整个Aplication程序。 If it's your case, you can just do nothing and let the form (window) close itself:如果是您的情况,您可以什么都不做,让表单(窗口)自行关闭:

private void StudentReg_FormClosing(object sender, FormClosingEventArgs e)
{
    // Cancel closing (i.e. do NOT close the form) if and only if user's chosen "No".
    // Do nothing (i.e. do not cancel) in other cases (let the form be closed)
    // So we assign true (cancel) if "No" has been pressed, false otherwise 
    e.Cancel = MessageBox.Show(
      "Do you really want to close this window?", 
      "Exit", 
       MessageBoxButtons.YesNo) == DialogResult.No; 
}

Depending on how you setup your application like if this is your main form or you have multiple you may need to just remove the event to avoid duplicate calls to it.根据您设置应用程序的方式,例如这是您的主表单还是您有多个,您可能只需要删除该事件以避免重复调用它。

Remove the event handler before calling application.exit and I think your issue will be solved.在调用 application.exit 之前删除事件处理程序,我认为您的问题将得到解决。

This is psuedo-code.这是伪代码。

private void StudentReg_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dialog = MessageBox.Show("Do you really want to close this window?", "Exit", MessageBoxButtons.YesNo);
            if(dialog == DialogResult.Yes) 
            {
                StudentReg.FormClosing -= StudentReg_FormClosing; 
                Application.Exit();
           
            }
            else if(dialog == DialogResult.No)
            {
                e.Cancel = true;

                }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得对另一个程序的对话框的引用,以便可以单击按钮 - How do I get a refernce to a dialog box of another program so that I can click a button 如何在用户关闭窗口之前检测到修改 - How do I detect a modification before the user closes the window 如何修复我的等待和重复代码? 我无法单击重复操作的按钮 - How Do I Fix My Wait And Repeat Code? I Cant Click The Button That Is Repeating an action 如何创建一个检查日期时间并在经过时间后执行操作的过程? - How do I create a process that checks date time and performs action when elapsed time passed? 如何修复“必须先修复所有编译器错误才能进入播放模式!” Unity中的错误 - How do I fix "All compiler errors have to be fixed before you can enter playmode!" error in Unity 我必须多次单击“消息框”按钮才能从消息框出来 - I have to click on a MessageBox Button several times to come out of the Message Box 为什么我必须为异步查询多次编写等待? - Why do I have to write await multiple times for an async query? 如何多次过滤Objectset? - How do I filter an Objectset multiple times? 如何在Silverlight中创建弹出对话框? - How do I create a Popup Dialog box in Silverlight? 如何使用jQuery在对话框中打开页面? - How do i Open a page in a dialog box using jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM