简体   繁体   English

C#中的窗体将DialogResult.Cancel最小化时返回给父对象

[英]Form in C# returns DialogResult.Cancel to Parent When It is Minimized

I have a form "Main" and in Main I am creating another form which I use .ShowDialog() to display it. 我有一个窗体“ Main”,在Main中,我正在创建另一个窗体,使用.ShowDialog()来显示它。 I do this because I don't want the rest of the code to in Main to execute until after the new form is completed. 之所以这样做,是因为在新表格完成之前,我不希望其余代码在Main中执行。 I am allowing a user to minimize the new form to system tray. 我允许用户最小化新表格到系统托盘。

The problem: When the form gets minimized it is returning DialogResult.Cancel to the Main form that called it, causing the next line to run early. 问题:当窗体最小化时,它将DialogResult.Cancel返回到调用它的Main窗体,导致下一行提前运行。

Code to create form from Main form: 从主表单创建表单的代码:

    for(int i = 0; i < lvAll.SelectedItems.Count; i++)
    {
        this.Hide();
        this.run = new RunProfile(this.profiles[lvAll.SelectedItems[i].Text]);
        DialogResult result = run.ShowDialog();
        MessageBox.Show(result.ToString());
    }

In the new form a user will get a list of files that copy... now the user can continue and copy those files and I would expect to return a result of OK and if not I assume they are going to cancel and return Cancel... 在新表单中,用户将获得要复制的文件的列表...现在,用户可以继续复制这些文件,我希望返回OK的结果,否则,我认为它们将要取消并返回Cancel。 ..

Is my only recourse to return Abort from the new form if a user specifies a cancel and assume that a cancel is someone trying to minimize? 如果用户指定取消并假设有人试图最小化取消,我是否唯一的途径是从新表单中返回中止? This just seems odd 这似乎很奇怪

This is entirely by design. 这完全是设计使然。 When you hide a modal dialog, the user doesn't have any way to get back to the program. 当您隐藏模式对话框时,用户将无法返回程序。 Dialogs don't have a taskbar button and the rest of the windows in the app are disabled so cannot be activated. 对话框没有任务栏按钮,应用程序中的其余窗口均被禁用,因此无法激活。 The only recourse the user would have is to kill your program with Task Manager. 用户拥有的唯一途径就是使用任务管理器终止程序。

So Winforms does the logical thing, it automatically closes the dialog to avoid this UI trap. 因此,Winforms会执行逻辑操作,它会自动关闭对话框以避免此UI陷阱。 And of course you'll get DialogResult.Cancel. 当然,您将获得DialogResult.Cancel。

Use proper UI design, a dialog should always have its MinimizeBox property set to False. 使用正确的UI设计,对话框应始终将其MinimizeBox属性设置为False。

I'm not sure what you are trying to achieve with that. 我不确定您要达到的目标。 However, to get the Dialog to return anything else, you have to set it in the form's (Dialog's) DialogResult property before the form is closed. 但是,要使Dialog返回其他任何内容,必须在关闭窗体之前在窗体的(Dialog)的DialogResult属性中进行设置。

You may also use the Form_Closing event of the dialog to set the DialogResult property to what you want. 您也可以使用对话框的Form_Closing事件将DialogResult属性设置为所需的属性。 This is done before the form closes. 这是在窗体关闭之前完成的。

You could handle this in a number of ways, one way is like what follows: 您可以通过多种方式处理此问题,一种方法如下所示:

    MainTestForm mainTestForm = new MainTestForm();

    if (mainTestForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
    {
        return;
    }

You must set the DialogResult state when you close the form like 当您关闭窗体时,您必须设置DialogResult状态

    this.DialogResult = System.Windows.Forms.DialogResult.OK;

I hope this helps. 我希望这有帮助。

Here is a quote from MSDN regarding ShowDialog: 这是MSDN关于ShowDialog的报价:

When this method is called, the code following it is not executed until after the dialog box is closed. 调用此方法时,直到关闭对话框后,该代码之后的代码才会执行​​。

After further reading it does state the X does not close the form but hides it and so you must dispose of it: 进一步阅读后,它会指出X不会关闭表单而是将其隐藏,因此您必须处理它:

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X in the top-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. 当窗体显示为模式对话框时,单击“关闭”按钮(窗体右上角带有X的按钮)将导致窗体被隐藏,并且DialogResult属性被设置为DialogResult.Cancel。 The Close method is not automatically called when the user clicks the Close button of a dialog box or sets the value of the DialogResult property. 当用户单击对话框的“关闭”按钮或设置DialogResult属性的值时,不会自动调用Close方法。 Instead, the form is hidden and can be shown again without creating a new instance of the dialog box. 而是隐藏该窗体,并且可以在不创建对话框的新实例的情况下再次显示该窗体。 Because of this behavior, you must call the Dispose method of the form when the form is no longer needed by your application. 由于这种行为,当您的应用程序不再需要该表单时,您必须调用该表单的Dispose方法。

It seems this same issues applies to minimizing a userform as well. 似乎相同的问题也适用于最小化用户表单。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM