在C#中如何在不打开表单的情况下运行winform [英] In C# how do I run a winform without opening the form

查看:72
本文介绍了在C#中如何在不打开表单的情况下运行winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForm C#program.cs文件。它(a)运行设置Form1或(b)加载设置文件。 Form1构造一个默认的安装文件,可以自定义并保存为序列化文本设置文件。 Form1有一个菜单项RUN。单击它时,Form1隐藏自己并运行/显示Form2。退出Form2时,它将自行关闭并显示Form1。所有这一切似乎都运行正常。



但我的问题是在情况b中,程序文件接收要加载的安装文件的路径名。之后,我想立即去Form2 ..没有显示Form1。我甚至不希望Form1闪烁,然后隐藏,这是我发现的关于这个或非常类似的问题的各种Web查询/建议的结果。



I尝试在program.cs中设置一个标志,这样当Form1运行时,它会直接跳转到RUN事件处理程序并显示Form2。但Form1并不隐藏。现在发生的是程序文件调用加载设置的例程,然后Form1运行,是可见的,并显示该设置,然后我的Form2运行也使用加载的设置,两者都可见。 Form1.Hide()调用似乎被忽略了。也许我应该尝试在这种情况下从program.cs运行Form2(但它最终必须返回到Form1)。感谢所有关于线索链接的建议。

I have a WinForm C# program.cs file. It either (a) runs a setup Form1 or (b) loads a setup file. Form1 constructs a default setup file that can be customized and saved as a serialized text setup file. Form1 has a menu item RUN. When that is clicked, Form1 hides itself and runs/shows Form2. When Form2 is exited, it closes itself and shows Form1. All this seems to work fine.

But my problem is that in situation "b", the program file receives the pathname of a setup file to load. After that I want to go immediately to Form2 .. without ever showing Form1. I don't even want Form1 to flicker on, then hide, which is the result of various Web queries/suggestions I found about this or very similar problems.

I try to set a flag in program.cs so that when Form1 runs, it jumps right to the RUN event handler and shows Form2. But Form1 doesn't hide. What happens now is that the program file calls a routine that loads the setup, then Form1 runs, is visible, and displays that setup and then my Form2 runs as well using the loaded setup and both are visible. Form1.Hide() calls seem ignored. Perhaps I should just try running Form2 from program.cs in this case (but it has to return to a Form1 eventually). Thank so for any suggestions of links to clues here.

推荐答案

据我所知,这看起来像是一个设计问题。您正试图控制表单中的工作流程,这些工作流程实际上是工作流程的一部分。因此你会遇到问题。



我建议你创建一个新类,负责在正确的时间打开并调用正确的表格。此类不应具有任何用户界面,而仅充当控制器。 Form1和Form2不应该彼此了解,但所有逻辑都应该通过控制类。



这样整个工作流程可以从任何点决定和启动即使不需要也不必担心是否加载了表单。
As far as I can see this looks like a design issue. You're trying to control the workflow inside the forms which actually are parts of the workflow. Because of this you encounter problems.

What I suggest is that you create a new class which would be responsible for opening and calling the correct form at the correct time. This class should not have any user interface but act only as a controller. The Form1 and Form2 should not know about each other but all the logic should go through the controlling class.

This way the whole workflow can be decided and initiated from any point without having to worry if a form is loaded even if not needed.


你的意思是以下内容?



You mean something like the following?

if ( setup file doesn't exist )
{
  run Form 1 to create setup
}

if ( setup file exists )
{
  run Form 2 on setup file
}


这篇关于在C#中如何在不打开表单的情况下运行winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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