动态按钮的点击事件 [英] click events for dynamic button arrrays
本文介绍了动态按钮的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何为动态按钮数组创建click事件?我已经找到了很多关于动态数组的答案,但是我无法为按钮数组实现它.任何人都可以帮我吗?动态按钮数组和它的click事件.但是它存在运行时错误,提示"btn_Click的不重载与委托System.EventHandler匹配",请帮帮我!
how can i create a click event for a dynamic button array?i have found lots of answers for dynamic arrays,but i cant implement it for a button array.can any one help me?following code represent how i have implemented the dynamic button array and a click event for it.but it have run time errors saying,"No overload for btn_Click matches delegate System.EventHandler" please help me!!!
private Button[] bu = new Button[12];
public static int index1;
public int[] ar = new int[4];
public string[] cl1 = new string[4];
index1 = 0;
{
x = 30;
for (int i = 0; i < ar.Length; i++)
{
bu[index1] = new Button();
bu[index1].Location = new Point(50, x);
bu[index1].Size = new Size(75, 20);
bu[index1].Text = "1";
cl1[i] = "b" + i;//name the buttons @ the 1st column
lable[index1] = new Label();
lable[index1].Location = new Point(75,(x+25));
lable[index1].Size = new Size(35, 13);
//lable[index1].Text = Convert.ToString(cl1[i]);
bu[index1].Name = Convert.ToString(cl1[i]);
lable[index1].Text = bu[index1].Name;
x = x + 100;
index1++;
}
this.Controls.AddRange(bu);
this.Controls.AddRange(lable);
bu[index1].Click += new EventHandler(btn_Click);
}
private void btn_Click()
{
MessageBox.Show("You hv cast ur vote for the 1ft preference");
}
推荐答案
我已经找到了很多有关动态数组的答案,但是我不能为按钮数组实现它.
谁说的?您是否看过我在此部分中共享的链接:按钮数组的通用事件处理程序 [ ^ ]
i have found lots of answers for dynamic arrays,but i cant implement it for a button array.
Who said so? Did you look at this link I shared, here this section: Creating a Common Event Handler for a button array[^]
您修改后的代码应该可以工作:
Hi,
this modified code of yours should work:
private Button[] bu = new Button[12];
public static int index1;
public int[] ar = new int[4];
public string[] cl1 = new string[4];
index1 = 0;
{
x = 30;
for (int i = 0; i < ar.Length; i++)
{
bu[index1] = new Button();
bu[index1].Location = new Point(50, x);
bu[index1].Size = new Size(75, 20);
bu[index1].Text = "1";
bu[index1].Click += new EventHandler(button_Click);
cl1[i] = "b" + i;//name the buttons @ the 1st column
lable[index1] = new Label();
lable[index1].Location = new Point(75,(x+25));
lable[index1].Size = new Size(35, 13);
//lable[index1].Text = Convert.ToString(cl1[i]);
bu[index1].Name = Convert.ToString(cl1[i]);
lable[index1].Text = bu[index1].Name;
x = x + 100;
index1++;
}
this.Controls.AddRange(bu);
this.Controls.AddRange(lable);
}
private void button_Click(object sender, EventArgs e)
{
MessageBox.Show("You hv cast ur vote for the 1ft preference");
}
这篇关于动态按钮的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文