<bdo id='2JMf0'></bdo><ul id='2JMf0'></ul>
    1. <legend id='2JMf0'><style id='2JMf0'><dir id='2JMf0'><q id='2JMf0'></q></dir></style></legend>

    2. <i id='2JMf0'><tr id='2JMf0'><dt id='2JMf0'><q id='2JMf0'><span id='2JMf0'><b id='2JMf0'><form id='2JMf0'><ins id='2JMf0'></ins><ul id='2JMf0'></ul><sub id='2JMf0'></sub></form><legend id='2JMf0'></legend><bdo id='2JMf0'><pre id='2JMf0'><center id='2JMf0'></center></pre></bdo></b><th id='2JMf0'></th></span></q></dt></tr></i><div id='2JMf0'><tfoot id='2JMf0'></tfoot><dl id='2JMf0'><fieldset id='2JMf0'></fieldset></dl></div>
      1. <tfoot id='2JMf0'></tfoot>

        <small id='2JMf0'></small><noframes id='2JMf0'>

        在 C# Winform 上使用显示对话框时非常奇怪的错误

        时间:2023-09-14
          <i id='iWdIu'><tr id='iWdIu'><dt id='iWdIu'><q id='iWdIu'><span id='iWdIu'><b id='iWdIu'><form id='iWdIu'><ins id='iWdIu'></ins><ul id='iWdIu'></ul><sub id='iWdIu'></sub></form><legend id='iWdIu'></legend><bdo id='iWdIu'><pre id='iWdIu'><center id='iWdIu'></center></pre></bdo></b><th id='iWdIu'></th></span></q></dt></tr></i><div id='iWdIu'><tfoot id='iWdIu'></tfoot><dl id='iWdIu'><fieldset id='iWdIu'></fieldset></dl></div>

            <bdo id='iWdIu'></bdo><ul id='iWdIu'></ul>

            <small id='iWdIu'></small><noframes id='iWdIu'>

            <legend id='iWdIu'><style id='iWdIu'><dir id='iWdIu'><q id='iWdIu'></q></dir></style></legend>
                <tbody id='iWdIu'></tbody>

                  <tfoot id='iWdIu'></tfoot>
                  本文介绍了在 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 上使用显示对话框时非常奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:UWP 中带有 3 个以上按钮的自定义内容对话框 下一篇:WPF 多线程进度对话框

                  相关文章

                      <i id='25HUI'><tr id='25HUI'><dt id='25HUI'><q id='25HUI'><span id='25HUI'><b id='25HUI'><form id='25HUI'><ins id='25HUI'></ins><ul id='25HUI'></ul><sub id='25HUI'></sub></form><legend id='25HUI'></legend><bdo id='25HUI'><pre id='25HUI'><center id='25HUI'></center></pre></bdo></b><th id='25HUI'></th></span></q></dt></tr></i><div id='25HUI'><tfoot id='25HUI'></tfoot><dl id='25HUI'><fieldset id='25HUI'></fieldset></dl></div>
                      <legend id='25HUI'><style id='25HUI'><dir id='25HUI'><q id='25HUI'></q></dir></style></legend>

                    1. <tfoot id='25HUI'></tfoot>

                      <small id='25HUI'></small><noframes id='25HUI'>

                        <bdo id='25HUI'></bdo><ul id='25HUI'></ul>