关闭对话框,形式推出的对话框 [英] Closing dialog box and form which launched the dialog box

查看:152
本文介绍了关闭对话框,形式推出的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms应用程序的形式。当我按下一个按钮,它加载与选项的模式消息框yes和no。

I have a form in a winforms app. When I press a button, it loads a modal message box with the options yes and no.

这是好的,但是当我按不,我要同时关闭。对话框,并在其中推出的对话框(发件人)按钮的形式

This is fine, but when I press no, I want to close both the dialog box and the form where the button which launched the dialog box (the sender) is.

因此,应用程序的结构是这样的:

So the app structure is like this:

主应用程序窗口>按菜单项,启动此表新表(连接设置)>按下按钮启动的消息框。

Main app window > press menu item to launch new form (connection setup) > press button on this form to launch message box.

两个窗口都打开(连接设置形式和对话框),我既想关闭。

Two windows are open (connection setup form and dialog box), which I both want closed.

如何我能做到这一点?

推荐答案

在您的是,没有模态形式,只要设置的DialogResult 来的没有的当您按下的没有的按钮,如:

In your yes-no modal form, just set DialogResult to No when you press the No button, like:

private void noButton_Click(object sender, EventArgs e)
{
    this.DialogResult = System.Windows.Forms.DialogResult.No;
}

和模态窗体将自动关闭,当你点击的

and the modal form will automatically close when you click No

然后当你打开你的模式窗体做这样的事情(在连接设置形式):

Then when you open your modal form do something like this (in the connection setup form):

var modalForm = new YesNoForm();
if (modalForm.ShowDialog() == DialogResult.No)
{
    this.Close(); // close the connection setup form
}



修改

我还以为你是 - 没有模态形式是定制的,如果它是一个简单的MessageBox,只是做:

I thought your yes-no modal form was custom, if it's a simple MessageBox, just do:

var dlgResult = MessageBox.Show("Yes or no ?","?",MessageBoxButtons.YesNo);
if(dlgResult == System.Windows.Forms.DialogResult.No)
{
    this.Close(); // close the connection setup form
}



在其他的答案已经建议

as already suggested in other answers

这篇关于关闭对话框,形式推出的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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