如何在动态创建的控件中添加事件 [英] how to add events in a dynamically created control

查看:75
本文介绍了如何在动态创建的控件中添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
请帮帮我.如何在c#.net
动态创建的超链接控件中添加事件
谢谢,
rumki

hi everyone,
please help me out.how to add events in a dynamically created hyperlink control in c#.net

thanks,
rumki

推荐答案

HyperLink hyperLink = new HyperLink();
hyperLink.Click += OnHyperLinkClick;


假定控件为Button并且其父级为Panel:

Assuming the control is Button and its parent is Panel:

Panel parent = new Panel();
//...

Button myButton = new Button();
myButton.Text = "My &Button";
myButton.Width = //...
myButton.Left = //...
myButton.Top = //...

parent.Controls.Add(myButton);

//now, the event; you don't need a separate method for it, use anonymous:
MyButton.Click += delegate(object sender, KeyEventArgs eventArgs) {
    Button buttonSender = (Button)sender; //can be sure
    //call whatever you want, using buttonSender and/or evenArgs or not
}

//easier way if you have C# v.3 or later, use lambda form:
MyButton.Click += (sender, eventArgs) => {
    Button buttonSender = (Button)sender; //can be sure
    //call whatever you want, using buttonSender and/or evenArgs or not
}
//if this form, you don't need argument types, they are inferred from the even type.



有关动机和讨论,请参见如何致电单击特定按钮上的keydown事件 [ ^ ].

—SA



For motivation and discussion, see also how to call keydown event on particular button click[^].

—SA


这篇关于如何在动态创建的控件中添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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