C#/.NET - WinForms - 实例化一个表单而不显示它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/807005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#/.NET - WinForms - Instantiate a Form without showing it
提问by BuddyJoe
I am changing the Visibility of a Form to false during the load event AND the form still shows itself. What is the right event to tie this.Visible = false; to? I'd like to instantiate the Form1 without showing it.
我在加载事件期间将表单的可见性更改为 false 并且表单仍然显示自己。什么是绑定 this.Visible = false; 的正确事件?到?我想实例化 Form1 而不显示它。
using System;
using System.Windows.Forms;
namespace TestClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
}
}
}
采纳答案by Fredrik M?rk
Regardless of how much you try to set the Visible property before the form has been shown, it will pop up. As I understand it, this is because it is the MainForm of the current ApplicationContext. One way to have the form automatically load, but not show at application startup is to alter the Main method. By default, it looks something like this (.NET 2.0 VS2005):
无论您在表单显示之前如何尝试设置 Visible 属性,它都会弹出。据我了解,这是因为它是当前 ApplicationContext 的 MainForm。让表单自动加载但在应用程序启动时不显示的一种方法是更改 Main 方法。默认情况下,它看起来像这样(.NET 2.0 VS2005):
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
If you instead do something like this, the application will start, load your form and run, but the form will not show:
如果您改为这样做,应用程序将启动,加载您的表单并运行,但表单不会显示:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 f = new Form1();
Application.Run();
}
I am not entirely sure how this is useful, but I hope you know that ;o)
我不完全确定这有什么用,但我希望你知道;o)
Update: it seems that you do not need to set the Visible property to false, or supply an ApplicationContext instance (that will be automatically created for you "under the hood"). Shortened the code accordingly.
更新:您似乎不需要将 Visible 属性设置为 false,也不需要提供 ApplicationContext 实例(将在“幕后”自动为您创建)。相应地缩短了代码。
回答by Anton Gogolev
Just create an instance of Form1
and do not call methods to show/display it. But I bet you're doing something wrong.
只需创建一个实例Form1
而不调用方法来显示/显示它。但我敢打赌你做错了什么。
回答by Eoin Campbell
Have you tried
你有没有尝试过
this.Hide();
in the form_load
or form_activated
events
在form_load
或form_activated
事件中
回答by David Suarez
Try on the VisibleChanged event.
尝试 VisibleChanged 事件。
回答by Nikos Steiakakis
What I would suggest would be to instantiate the form in an event the precedes the _Show event, such as the constructor, after the IntializeComponent() call.
我的建议是在 IntializeComponent() 调用之后在 _Show 事件之前的事件中实例化表单,例如构造函数。
回答by Chad Grant
Set the visibilty on the constructor, after init and then this.Show() later
在构造函数上设置可见性,在 init 之后,然后在 this.Show() 之后
回答by Chris Persichetti
The shown event may give you want you want. Although the form will "flash" for a second before hiding.
显示的事件可能会给你想要的。虽然表格在隐藏之前会“闪烁”一秒钟。
private void Form1_Shown(object sender, EventArgs e)
{
this.Visible = false;
}
回答by Matthew Olenik
InitializeComponent() is setting this.Visible = true, since you specified that the form should be visible in the designer (or it defaulted to that). You should set Visible to false in the designer, and it won't be called by InitializeComponent(). You can then make it visible any time you like.
InitializeComponent() 正在设置 this.Visible = true,因为您指定表单在设计器中应该是可见的(或者它默认为)。您应该在设计器中将 Visible 设置为 false,并且它不会被 InitializeComponent() 调用。然后,您可以随时使其可见。
回答by Adam Robinson
Having .Visible = false
or Hide()
in the Load
event will cause your form to show briefly, as there is time between when it becomes physically visible and when the Load
event gets fired, in spite of the fact that the documentation says the contrary.
有.Visible = false
或Hide()
在Load
事件中会导致您的表单短暂显示,因为在它变得物理可见和Load
事件被触发之间有一段时间,尽管文档说明相反。
Are you calling Show()
or ShowDialog()
somewhere? I'm not sure if this behavior is still present, but at least in past versions of the framework a call to ShowDialog()
did not trigger the Load
event, so perhaps that is your issue (though I think calling ShowDialog()
then hiding a modal form would be a bad practice!)
你在打电话Show()
还是在ShowDialog()
某个地方?我不确定这种行为是否仍然存在,但至少在框架的过去版本中,调用ShowDialog()
并没有触发该Load
事件,所以这可能是您的问题(尽管我认为调用ShowDialog()
然后隐藏模态表单会很糟糕)实践!)
If you have to have the handle created (and the handles for controls) for whatever it is you're trying to do, a better idea would be to set the StartLocation
to Manual
, then set the Position
property to an offscreen location. This will create and show the form, while making it invisible to the user.
如果您必须为要执行的任何操作创建句柄(以及控件的句柄),更好的主意是将 设置StartLocation
为Manual
,然后将Position
属性设置为屏幕外位置。这将创建并显示表单,同时使其对用户不可见。
回答by automatic
If this is your main form, there may not be a better place then the Shown event. But in that case you will get flicker.
如果这是您的主要形式,那么 Shown 事件可能没有比这更好的地方了。但在这种情况下,你会得到闪烁。
I couldn't find a good place to stop a running main form from showing at least quickly. Even a timer activated in the load event won't do it.
我找不到一个好地方来阻止正在运行的主窗体至少快速显示。即使在加载事件中激活的计时器也不会这样做。
If it is a secondary form just create it but don't show it.
如果它是辅助表单,只需创建它但不要显示它。