Very strange bug when using Show Dialog on C# Winform(在 C# Winform 上使用显示对话框时非常奇怪的错误)
问题描述
我在 VS Studio 2008 Express Edition 中创建了 2 个表单,并在 main program.cs 文件中使用 public static 声明它们
I have created 2 forms in VS Studio 2008 Express Edition and declare them with public static in main program.cs file
我只想使用 ShowDialog 和 Close 在两个表单之间切换,但是当尝试关闭第二个表单并使用 showdialog 再次打开第一个表单时,它说当表单已经可见时我不能使用 showDialog,而它不是真的,因为我之前关闭它以显示第二个表单.
I just want to switch between the two forms with ShowDialog and Close but when trying to close the second form and open the first form again with showdialog it says I cannot use showDialog when the form is already visible, whereas it isn't true since I closed it before to show the second form.
它要求我在使用 showdialog 之前将表单可见属性设置为 false,所以我这样做了
It asked me to set the form visible property to false before using showdialog, so I did it
internal static void CloseSecondForm(FirstForm FirstForm)
{
FirstForm .Close();
SecondForm.Visible = false;
SecondForm.ShowDialog();
}
然后它说我不能使用 ShowDialog,因为表单已经以对话模式显示,我必须关闭它.所以我按照它的要求做了
But then it says I cannot use ShowDialog because the form is already shown in Dialog Mode and that I must close it. So I did what it asked
internal static void CloseSecondForm(FirstForm FirstForm)
{
FirstForm .Close();
SecondForm.Visible = false;
SecondForm.Close();
SecondForm.ShowDialog();
}
但它仍然假装表单已经用 ShowDialog 打开了!
But it still pretends that the form is already opened with ShowDialog !
这是我的 prog 或 Winform 中的错误吗?
Is this a Bug in my prog or in Winform ?
更新:这是我在第 5 个答案中发布的全部代码(我想使用 showdialog 而不是显示,因为我可能在后台有第 3 个表单,我不希望用户访问):
Update: this is the whole code I posted in 5th answer (I want to use showdialog and not show because I may have a 3rd form in Background that I don't want the user to access):
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 = new Form1();
Form2 = new Form2();
Form1.ShowDialog();
Application.Run();
}
// called from Form1 BUTTON
internal static void ShowForm2(Form1 Form1)
{
Form1.Hide();
Form2.ShowDialog();
}
// called from Form2 BUTTON
internal static void ShowForm1(Form2 Form2)
{
Form2.Hide();
Form1.ShowDialog();
}
<小时>
我按照建议尝试了隐藏,但它也不起作用.这是整个程序,我想做的很简单:我最初在程序中创建了两个表单,每个表单上都有一个按钮来关闭自己并打开另一个.我把所有的逻辑放在下面的program.cs中:
I tried with Hide as suggested but it doesn't work either. This is the whole program, what I want to do is very simple: I have two forms initially created in program with one button on each form to close self and open the other. I put all the logic in program.cs below:
using System;
using System.Windows.Forms;
namespace twoforms
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
public static Form1 Form1;
public static Form2 Form2;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 = new Form1();
Form2 = new Form2();
Form1.ShowDialog();
Application.Run();
}
// called from Form1 BUTTON
internal static void ShowForm2(Form1 Form1)
{
Form1.Hide();
Form2.ShowDialog();
}
// called from Form2 BUTTON
internal static void ShowForm1(Form2 Form2)
{
Form2.Hide();
Form1.ShowDialog();
}
}
}
推荐答案
这是来自 MSDN:
当表单显示为模式时对话框,单击关闭按钮(带有 X 的按钮表格的右上角)导致要隐藏的表格和DialogResult 属性设置为DialogResult.Cancel.不像无模式窗体,不调用 Close 方法当用户由 .NET Framework单击 a 的关闭表单按钮对话框或设置的值DialogResult 属性.取而代之的是表格被隐藏,可以再次显示无需创建新的实例对话框.因为显示了一个表格作为一个对话框没有关闭,你必须调用 Dispose 方法不再需要表格时的表格由您的应用程序.
When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Unlike modeless forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application.
因此,一旦您使用 ShowDialog 显示表单并且现在想要关闭它,只需让它返回 DialogResult.Cancel这将隐藏(它仍将在内存中)您的第一个表单.现在您可以在第二个表单上调用 ShowDialog.同样,如果您想切换到第一个表单,那么让第二个表单返回 DialogResult.Cancel,现在只需在第一个表单上调用 ShowDialog.
So once you show a form using ShowDialog and you now want to close it, just let it return DialogResult.Cancel This will hide (it will still be in memory) your first form. Now you can call ShowDialog on your second form. Again, if you want to switch to first form then let the second form return DialogResult.Cancel and now just call ShowDialog on first form.
这篇关于在 C# Winform 上使用显示对话框时非常奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# Winform 上使用显示对话框时非常奇怪的错误
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01