动态生成控件 动态生成Buttong控件后,如何在参数的传递中调用相应的单击事件。请各位大侠帮忙! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 手动加就行this.button1.Click += new EventHandler(button1_Click); 1 你可以加一个时间 对参数 进行判断 private void button_Click(object sender, EventArgs e) { Button _Button = (Button)sender; MessageBox.Show(_Button.Text); }2 或则 你先定义好事件 EventHandler _Event1 = new EventHandler(button1_Click); EventHandler _Event2 = new EventHandler(button2_Click); for (int i = 0; i != 2; i++) { Button _Button = new Button(); _Button.Size = new Size(100, 100); _Button.Location = new Point(0, i * 100); if (i == 0) { _Button.Click += _Event1; } else { _Button.Click += _Event2; } this.Controls.Add(_Button); _Button.BringToFront(); } 2楼: 因为事先不知会动态生成几个Button控件,所以怎么能事先定义好事件呢。请再说的详细点。 2楼说得很详细了,其实就是这句this.button1.Click += new EventHandler(button1_Click);把事件和事件处理方法关联起来。每生成一个控件,就需要关联它的事件和事件处理方法。 /// <summary> /// 给控件事件添加处理方法 /// </summary> public static void AddClickFunction(System.Windows.Forms.Control.ControlCollection Controls) { foreach (Control control in Controls) { if (control.GetType().ToString() == "System.Windows.Forms.Button") { Button lb = new Button(); lb = (Button)control; lb.Click+=事件处理方法; } } } } 你可以添加一个Button就给它加一个Click事件呀,那样不是可以需要了。既然不知道添加多少,那这些Button的事件应该有所相似的,可以通过变量传递嘛。 多个button 可以绑定到同一个方法,你可以设置一个标志,根据标志的不同就可以知道执行那个事件。 引用 3 楼 lihaiying616 的回复:2楼: 因为事先不知会动态生成几个Button控件,所以怎么能事先定义好事件呢。请再说的详细点。 2楼说得很详细了,其实就是这句this.button1.Click += new EventHandler(button1_Click);把事件和事件处理方法关联起来。每生成一个控件,就需要关联它的事件和事件处理方法。 /// <summary> /// 给控件事件添加处理方法 /// </summary> public static void AddClickFunction(System.Windows.Forms.Control.ControlCollection Controls) { foreach (Control control in Controls) { if (control.GetType().ToString() == "System.Windows.Forms.Button") { Button lb = new Button(); lb = (Button)control; lb.Click+=事件处理方法; } } } }----------------------------------------------------- 怎么能保证窗口最大化的时候宽不变? 求HTML站点地图的设计思路或代码 linq for gnu gcc? SOCKET 如何穿透代理连接到服务器,具体描述进来看,急 开发简易多线程服务器的问题 DataGridView的列的筛选! A窗体如何调用B窗体中一个button的事件?最好有简单代码 Remoting在com+组件中老是不行,帮我看看 在进程中改变Form1窗体上面Lable控件的Text的值,该怎么样实现修改呀? 请教.net向oracle插入中文的问题,很急,请大家帮忙看看 C# 如何操作 与 或 非 使用多线程,进程来处理数据
this.button1.Click += new EventHandler(button1_Click);
private void button_Click(object sender, EventArgs e)
{
Button _Button = (Button)sender; MessageBox.Show(_Button.Text);
}2 或则 你先定义好事件 EventHandler _Event1 = new EventHandler(button1_Click);
EventHandler _Event2 = new EventHandler(button2_Click); for (int i = 0; i != 2; i++)
{
Button _Button = new Button();
_Button.Size = new Size(100, 100);
_Button.Location = new Point(0, i * 100); if (i == 0)
{
_Button.Click += _Event1;
}
else
{
_Button.Click += _Event2;
}
this.Controls.Add(_Button);
_Button.BringToFront();
}
因为事先不知会动态生成几个Button控件,所以怎么能事先定义好事件呢。请再说的详细点。
2楼说得很详细了,其实就是这句this.button1.Click += new EventHandler(button1_Click);把事件和事件处理方法关联起来。每生成一个控件,就需要关联它的事件和事件处理方法。
/// <summary>
/// 给控件事件添加处理方法
/// </summary>
public static void AddClickFunction(System.Windows.Forms.Control.ControlCollection Controls)
{
foreach (Control control in Controls)
{
if (control.GetType().ToString() == "System.Windows.Forms.Button")
{
Button lb = new Button();
lb = (Button)control;
lb.Click+=事件处理方法;
}
}
}
}
既然不知道添加多少,那这些Button的事件应该有所相似的,可以通过变量传递嘛。
多个button 可以绑定到同一个方法,你可以设置一个标志,根据标志的不同就可以知道执行那个事件。
2楼:
因为事先不知会动态生成几个Button控件,所以怎么能事先定义好事件呢。请再说的详细点。
2楼说得很详细了,其实就是这句this.button1.Click += new EventHandler(button1_Click);把事件和事件处理方法关联起来。每生成一个控件,就需要关联它的事件和事件处理方法。
/// <summary>
/// 给控件事件添加处理方法
/// </summary>
public static void AddClickFunction(System.Windows.Forms.Control.ControlCollection Controls)
{
foreach (Control control in Controls)
{
if (control.GetType().ToString() == "System.Windows.Forms.Button")
{
Button lb = new Button();
lb = (Button)control;
lb.Click+=事件处理方法;
}
}
}
}
-----------------------------------------------------