在C#WinForm的使用显示对话框时,很奇怪的错误 [英] Very strange bug when using Show Dialog on C# Winform

查看:231
本文介绍了在C#WinForm的使用显示对话框时,很奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS Studio 2008速成版创建的2种形式,并与主Program.cs的公共静态声明这些文件



我只想两种形式之间切换用的ShowDialog和关闭,但试图关闭第二种形式与ShowDialog的它说当窗体已经可见我不能使用的ShowDialog再次打开第一种形式,而这是不正确的,因为我之前关闭它,以显示第二种形式的时候。



这让我使用ShowDialog的前设置窗体可见属性设置为false,所以我做到了。

 内部静态无效CloseSecondForm(FirstForm FirstForm)
{
FirstForm .Close();
SecondForm.Visible = FALSE;
SecondForm.ShowDialog();
}



但后来它说我不能使用的ShowDialog因为表单在对话框已经显示模式,我必须关闭它。所以,我没有什么要求

 内部静态无效CloseSecondForm(FirstForm FirstForm)
{
FirstForm .Close ();
SecondForm.Visible = FALSE;
SecondForm.Close();
SecondForm.ShowDialog();
}



但它仍然假装形式已经与ShowDialog的开了!



这是在我的前卫或WinForm的一个Bug



更新:这是整个代码,我张贴在5回答(我想用的ShowDialog并没有显示,因为我可能有一个背景第三形式,我不希望用户访问):

  [STAThread] 
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
= Form1的Form1的新();
窗体2 =新Form2的();
Form1.ShowDialog();
Application.Run();

}

//从Form1中BUTTON
称为内部静态无效ShowForm2(Form1的Form1中)
{
Form1.Hide();
Form2.ShowDialog();
}

//从窗体2 BUTTON
内部静态无效ShowForm1(Form2的窗体2)
{
Form2.Hide()调用;
Form1.ShowDialog();
}






我试图隐藏的建议,但也不起作用。这是整个计划,我想要做的是非常简单的:我有初步方案,每个表单上的一个按钮创建两种形式来关闭自己和打开其他。我把所有的逻辑的Program.cs如下:

 使用系统;使用System.Windows.Forms的
;

命名空间twoforms
{
静态类节目
{
///<总结>
///的主入口点应用程序。
///< /总结>
///
公共静态Form1的Form1中;
公共静态Form2的Form2的;

[STAThread]
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
= Form1的Form1的新();
窗体2 =新Form2的();
Form1.ShowDialog();
Application.Run();

}

//从Form1中BUTTON
称为内部静态无效ShowForm2(Form1的Form1中)
{
Form1.Hide();
Form2.ShowDialog();
}

//从窗体2 BUTTON
内部静态无效ShowForm1(Form2的窗体2)
{
Form2.Hide()调用;
Form1.ShowDialog();
}
}
}


解决方案

这是从MSDN:




当一个表单显示为模式
对话框中,单击关闭按钮
(与在
中的形式的右上角的X按钮)导致
键被隐藏的形式和
DialogResult属性被设置为
DialogResult.Cancel。不同于无模式
形式,Close方法不叫由.NET Framework
当用户
点击一个
对话框关闭表单按钮或设置$的价值b $ b DialogResult属性。代替
形式是隐藏的,可以示出再次
而不创建
对话框的新实例。因为形式显示出来
作为一个对话框,不是封闭的,你
必须调用时不再需要的形式
。通过应用程序中的
形式的Dispose方法。




所以一旦你表现出的形式使用ShowDialog的,你现在要关闭它,就让它返回DialogResult.Cancel
这将隐藏(它仍然是在内存中)的第一种形式。现在,你可以调用的ShowDialog你的第二个形式。同样,如果你想切换到第一种形式则让第二种形式返回DialogResult.Cancel现在只是调用ShowDialog的第一个形式。


I have created 2 forms in VS Studio 2008 Express Edition and declare them with public static in main program.cs file

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.

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();
    }

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();
    }

But it still pretends that the form is already opened with ShowDialog !

Is this a Bug in my prog or in Winform ?

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();
  }


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();
          }
      }
  }

解决方案

This is from MSDN:

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.

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的使用显示对话框时,很奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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