C# 如何添加表单加载事件(目前不起作用)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9847376/
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
How to add a form load event (currently not working)
提问by user710502
I have a Windows Forms form where I am trying to show a user control when the form loads. Unfortunately, it is not showing anything. What am I doing wrong? Please see the code below:
我有一个 Windows 窗体窗体,我试图在窗体加载时显示用户控件。不幸的是,它没有显示任何内容。我究竟做错了什么?请看下面的代码:
AdministrationView wel = new AdministrationView();
public ProgramViwer()
{
InitializeComponent();
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
Please note I added the load event based on what I read in this article:
请注意,我根据我在本文中阅读的内容添加了加载事件:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspx
采纳答案by Bridge
Three ways you can do this - from the form designer, select the form, and where you normally see the list of properties, just above it there should be a little lightning symbol - this shows you all the events of the form. Find the form load event in the list, and you should be able to pick ProgramViwer_Loadfrom the dropdown.
您可以通过三种方式执行此操作 - 从表单设计器中选择表单,然后在您通常看到属性列表的位置,在其上方应该有一个小闪电符号 - 这会向您显示表单的所有事件。在列表中找到表单加载事件,您应该可以ProgramViwer_Load从下拉列表中进行选择。
A second way to do it is programmatically - somewhere (constructor maybe) you'd need to add it, something like: ProgramViwer.Load += new EventHandler(ProgramViwer_Load);
第二种方法是以编程方式 - 在某处(可能是构造函数)您需要添加它,例如: ProgramViwer.Load += new EventHandler(ProgramViwer_Load);
A third way using the designer (probably the quickest) - when you create a new form, double click on the middle of it on it in design mode. It'll create a Form load event for you, hook it in, and take you to the event handler code. Then you can just add your two lines and you're good to go!
使用设计器的第三种方式(可能是最快的) - 当您创建一个新表单时,在设计模式下双击它的中间。它将为您创建一个表单加载事件,将其挂入,然后将您带到事件处理程序代码。然后你可以添加你的两行,你就可以开始了!
回答by GETah
You got half of the answer! Now that you created the event handler, you need to hook it to the form so that it actually gets called when the form is loading. You can achieve that by doing the following:
你得到了一半的答案!现在您创建了事件处理程序,您需要将它挂接到表单上,以便在加载表单时实际调用它。您可以通过执行以下操作来实现这一点:
public class ProgramViwer : Form{
public ProgramViwer()
{
InitializeComponent();
Load += new EventHandler(ProgramViwer_Load);
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
}

