向动态添加的控件添加事件 [英] Add events to controls added dynamically

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

问题描述

我正在开发Winform应用程序。并且我动态添加了一些控件,例如。 按钮现在,我想向创建的按钮添加一个事件,该如何执行呢?还可以有人给我推荐一本涵盖了winform所有主题的 C#书吗?

I am working on winform app. and I have added some controls dynamicaly eg. Button now i want to add an event to that created button, how can i perform this? also can some one refer a C# book to me which has covered well all topics in winform? thanks.

推荐答案

// create some dynamic button
Button b = new Button();
// assign some event to it
b.Click += (sender, e) => 
{
    MessageBox.Show("the button was clicked");
};
// add the button to the form
Controls.Add(b);

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

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