将显示的事件添加到C#中的UserControl [英] Add Shown event to a UserControl in C#

查看:334
本文介绍了将显示的事件添加到C#中的UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个用户控件,我需要在事件上编辑一些类似于表单的显示"事件.

我尝试了以下操作,但不起作用:

将以下行添加到设计器中:

Hi,

I have a user control and i need to edit some things on the event similar to "Shown" with forms.

I tried the following but that does not work:

Added the following line to the designer:

this.Shown += this.HandleControlShown



并将其添加到控件的类中:



and added this to the class of the control:

public event EventHandler Shown;

private void HandleControlShown(object sender, EventArgs e)
{
    this.OnShown(EventArgs.Empty);
    //code to execute on shown event goes here.... but does not execute
}

protected virtual void OnShown(EventArgs e)
{
    EventHandler handler = this.Shown;
    if (handler != null)
    {
        handler(this, e);
    }
}



我在哪里做错了什么?



Where am i doing something wrong?

推荐答案

事件声明代码,可能对您有所帮助.
Eevent declaration code, may be helpful to you.
//declare a delegate
public delegate ShownEventHandler(string args);

public event ShownEventHandler Shown;
private void HandleControlShown(object sender, EventArgs e)
{
 if(this.Shown!=null)
 {
    this.Shown(String.Empty/*your args*/);
    //code to execute on shown event goes here.... but does not execute
 }
}


这篇关于将显示的事件添加到C#中的UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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