加载形式没有显示它 [英] Load a form without showing it

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

问题描述

短版:我想未做形式可见触发的Form_Load()事件。这不起作用,因为展()忽略Visible属性的当前值:

Short version: I want to trigger the Form_Load() event without making the form visible. This doesn't work because Show() ignores the current value of the Visible property:

tasksForm.Visible = false;
tasksForm.Show();

长版:我有两种形式WinForms应用程序:主要任务。始终显示的主要形式。用户可以单击一个按钮打开任务形式,或者单击干脆直接运行任务的一些按钮,无需打开任务形式。

Long version: I have a WinForms application with two forms: main and tasks. The main form is always displayed. The user can either click a button to open the tasks form, or click some buttons that just run a task directly without opening the tasks form.

当用户请求直接运行的任务,我想只是呼吁任务的一些公共方法形成没有显示它。不幸的是,任务逻辑要看的东西,发生在Form_Load()事件。我能找到触发的Form_Load()的唯一方法是调用展()。我已经能够做的最好的是显示在最小化状态的形式:

When a user asks to run a task directly, I'd like to just call some public methods on the tasks form without showing it. Unfortunately, the task logic depends on stuff that happens in the Form_Load() event. The only way I can find to trigger Form_Load() is to call Show(). The best I've been able to do is to show the form in the minimized state:

tasksForm.WindowState = FormWindowState.Minimized;
tasksForm.Show();

我想最干净的解决办法是拉任务的逻辑出了任务的形式,并成为一个控制器类。然后,我可以使用该类从主窗体,然后在任务形式,只有当我需要它的用户可见装载的任务形式。但是,如果它是一件容易的事情加载形式,而不显示出来,这将是一个更小的变化。

I suppose the cleanest solution would be to pull the tasks logic out of the tasks form and into a controller class. Then I can use that class from the main form and from the tasks form, and only load the tasks form when I need it visible for the user. However, if it's an easy thing to load the form without displaying it, that would be a smaller change.

推荐答案

我完全丰富,乙双方同意,你需要看看你在哪里把你的应用程序逻辑,而不是试图cludge的WinForms的机制。所有这些操作和数据,你的任务形式暴露确实应该在一个单独的类的说某种应用程序控制器或某事您的主要形式举行,然后通过你的任务的形式使用的读取和显示数据需要的,但时不需要一种形式来实例化存在。

I totally agree with Rich B, you need to look at where you are placing your application logic rather than trying to cludge the WinForms mechanisms. All of those operations and data that your Tasks form is exposing should really be in a separate class say some kind of Application Controller or something held by your main form and then used by your tasks form to read and display data when needed but doesn't need a form to be instantiated to exist.

这可能看起来很痛苦的返工,但你会提高应用程序的结构,使其更易于维护等。

It probably seems a pain to rework it, but you'll be improving the structure of the app and making it more maintainable etc.

这篇关于加载形式没有显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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