如何在新表单关闭之前暂停执行? [英] How can I make execution pause until new form is closed?

查看:32
本文介绍了如何在新表单关闭之前暂停执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Win Forms 应用程序以了解更多信息,因为我对它没有太多经验.在我的程序中,在主窗体中,我有一个按钮.单击它会启动另一个表单.代码如下:

I am making a Win Forms application to learn more since I don't have much experience with it. In my program, in the main form, I have a button. Clicking it launches another form. The code is as follows:

 private void btn_AddCat_Click(object sender, EventArgs e)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                form_NewCat NewCatForm = new form_NewCat();
                NewCatForm.Show();
            }));

            MessageBox.Show("Oops!");            
        }

问题是,当新表单启动时,我希望主表单后面的代码的执行在该点暂停,直到新表单关闭.例如,在上面的代码中,我不想要哎呀!"打印,直到新表单关闭.我怎样才能做到这一点?

The problem is, when the new form is launched, I want execution of the code behind the main form to pause at that point until the new form is closed. As an example, in the above code, I do not want 'Oops!' to get printed until the new form is closed. How can I achieve that?

推荐答案

当您在 UI 线程中时,您不需要调用.您在按钮单击事件处理程序的 UI 线程中.

You don't need to invoke when you are in the UI thread. And you are in the UI thread in a button-click eventhandler.

private void btn_AddCat_Click(object sender, EventArgs e)
{
    form_NewCat NewCatForm = new form_NewCat();
    var dialogResult = NewCatForm.ShowDialog();
    MessageBox.Show("Oops!");            
}

如果您的 form_NewCat 在关闭之前将 this.DialogResult 设置为任何值,您可以检查 dialogResult 的 OK、Cancel、Yes、No 等.这是指示用户如何退出表单/对话框的常用方式.

You can check the dialogResult for OK, Cancel, Yes, No, etc if your form_NewCat sets this.DialogResult to any value before closing. This is the usual way to signal how the user exited the form/dialog.

这篇关于如何在新表单关闭之前暂停执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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