覆盖表单的onload()函数。 [英] Override the onload() function of a form.

查看:122
本文介绍了覆盖表单的onload()函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





每个表单都有一个名为OnLoad()的方法,在其中可以对其进行编程,以便在加载表单时执行某些操作。



我打算覆盖类窗体的OnLoad()函数,然后在里面弹出一个消息框。



我该怎么办?



我尝试过:



我还在读它,但我还没找到解决办法。

Hi,

Every form has a method named OnLoad(), which inside it it can be programmed to do some behaviour when the form loaded.

I'm planning to override the OnLoad() function of the class form and then inside it it does something like popping up a messagebox.

How can I do that?

What I have tried:

I'm still reading about it and yet I've not found a solution.

推荐答案

是的,你可以覆盖Form OnLoad事件:
Yes, you can override the Form OnLoad event:
protected override void OnLoad(EventArgs e)
{
   base.OnLoad(e);
   MessageBox.Show("click okay and the Form will appear");
}

但是,你真的希望用户在启动应用程序时只看到MessageBox吗?如果你想首先显示表单,然后放一个MessageBox,试试这个:

But, do you really want the user to see nothing but a MessageBox when they start the app ? If you want to first show the Form, and then put up a MessageBox, try this:

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    MessageBox.Show("Are you ready ?");
}

但是,我想知道为什么......特殊情况需要......你......需要这样做,因为:



只需在设计模式下双击Form design-surface就会导致自动生成Form Load EventHandler:

But, I wonder why it is ... what special circumstances require ... you ... need to do this, because:

Just double-clicking on the Form design-surface in design-mode will cause the automatic generation of a Form Load EventHandler:

private void Form1_Load(object sender, EventArgs e)
{
    // do whatever ...
}

这是有线的表单的格式...您可以查看Designer.cs文件并查看如何建立连接:

which is wired-up to the Form ... you can look in the Designer.cs file and see how the connection is made:

this.Load += new System.EventHandler(this.Form1_Load);

或者,你可以定义实例化(创建)您自己的EventHandler并将其连接到表单:

Or, you can define instantiate (create) your own EventHandler and wire it up to the Form:

public Form1()
{
    InitializeComponent();
    this.Load += MyOnLoad;
}

private void MYOnLoad(object sender, EventArgs e)
{
    // do whatever
}

或者,你可以为'Shown事件定义一个EventHandler,如第二个代码示例所示,以便在MessageBox出现之前出现Form。

Or, you can define an EventHandler for the 'Shown event, as shown in the second code example here in order to the Form appear before the MessageBox appears.


参见 Form.OnLoad方法(EventArgs)(System.Windows.Forms) [ ^ ]。


这篇关于覆盖表单的onload()函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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