显示隐藏窗体 [英] Showing a hidden form

查看:163
本文介绍了显示隐藏窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要如何呈现从已使用隐藏

How do i show a from that have been hidden using

this.Hide();



我曾尝试

I have tried

MainMenuForm.Show();



这只是说我需要一个对象参考。然后,我尝试:

and this just says i need an object ref. I then tried:

MainMenuForm frmMainMenu = new MainMenuForm();
frmMainMenu.Show();



这似乎显示出相应的表格。但是,当你退出该应用程序,它仍然保存在内存中,因为它没有显示被隐藏的形式,而是呈现出形式的新版本。实际上具有形式的两个实例(一个隐藏的,一个可见)。

Which seems to show the appropriate form. But when you exit the app, it is still held in memory because it hasn't shown the form that was hidden, instead it has shown a new version of the form. In effect having 2 instances of the form (one hidden, one visible).

只是为了澄清,该MainMenuForm是启动窗体。当(例如)选项1被点击,然后MainMenuForm自己隐藏在打开了选项1的形式。我想知道是如何我使Option 1形式的MainMenuForm打开取消隐藏的MainMenuForm然后关闭本身。

Just to clarify, the MainMenuForm is the startup form. When (for example) Option 1 is clicked, the MainMenuForm then hides itself while opening up the Option 1 form. What i would like to know is how to i make the Option 1 form that the MainMenuForm opens "unhide" the MainMenuForm and then close itself.

这里有什么正确的程序?

What's the correct procedure here?

在此先感谢

推荐答案

当您执行以下操作:

MainMenuForm frmMainMenu = new MainMenuForm();
frmMainMenu.Show();

您正在创建和呈现出的的MainMenuForm的实例。

You are creating and showing a new instance of the MainMenuForm.

为了显示和隐藏你需要保持对它的引用了MainMenuForm的一个实例。即当我做Compact Framework的应用程序,我已经使用Singleton模式,以确保静态类我永远只能有一个表格的一个实例在运行时:

In order to show and hide an instance of the MainMenuForm you'll need to hold a reference to it. I.e. when I do compact framework apps, I have a static classes using the singleton pattern to ensure I only ever have one instance of a form at run time:

public class FormProvider
{
   public static MainMenuForm MainMenu
   {
       get
       {
          if (_mainMenu == null)
          {
            _mainMenu = new MainMenuForm();
          }
          return _mainMenu;
       }
   }
   private static MainMenuForm _mainMenu;
}

现在你可以使用 FormProvider.MainMenu.Show ()来显示窗体和 FormProvider.MainMenu.Hide()来隐藏窗体。

Now you can just use FormProvider.MainMenu.Show() to show the form and FormProvider.MainMenu.Hide() to hide the form.

借助 Singleton模式(感谢的拉撒路中的链接)是管理中的WinForms应用形式,因为这意味着你只有一次创建窗体实例的好方法。第一次的形式是通过其各自的属性来访问,表单被实例化并存储在一个专用变量。

The Singleton Pattern (thanks to Lazarus for the link) is a good way of managing forms in WinForms applications because it means you only create the form instance once. The first time the form is accessed through its respective property, the form is instantiated and stored in a private variable.

例如,你可以使用 FormProvider.MainMenu 第一次,私有变量_mainMenu被实例化。你叫 FormProvider.MainMenu 随后的任何时间,_mainMenu是直接返回远没有被再次实例化。

For example, the first time you use FormProvider.MainMenu, the private variable _mainMenu is instantiated. Any subsequent times you call FormProvider.MainMenu, _mainMenu is returned straight away without being instantiated again.

不过,你不必所有的表单类存储在一个静态实例。你可以只是形式作为公司控制的MainMenu窗体上的一个属性。

However, you don't have to store all your form classes in a static instance. You can just have the form as a property on the form that's controlling the MainMenu.

public partial class YourMainForm : Form
{
   private MainMenuForm _mainMenu = new MainMenuForm();

   protected void ShowForm()
   {
      _mainMenu.Show();
   }

   protected void HideForm()
   {
      _mainMenu.Hide();
   }
}



更新:

刚读 MainMenuForm 是启动窗体。实施类似我上面单身例子类,然后更改您的代码在应用程序的Program.cs文件如下:

Just read that MainMenuForm is your startup form. Implement a class similar to my singleton example above, and then change your code to the following in the Program.cs file of your application:

Application.Run(FormProvider.MainMenu);

您可以再访问 MainMenuForm 从任何地方在通过 FormProvider 类应用程序。

You can then access the MainMenuForm from anywhere in your application through the FormProvider class.

这篇关于显示隐藏窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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