如何在C#.NET在动态创建的按钮执行单击事件 [英] how to execute click event on dynamically created button in c#.net
问题描述
我想建立一个应用程序,在这里用户可以选择的类别,并根据它显示它的子类,这些子类别的按钮,这是动态创建的。
I am trying to build an app, where user can select category and according to it displays its sub categories , these sub categories are buttons, which are dynamically created.
现在,因为按钮是动态创建的,所以我混淆如何写button_click事件下的code,因为我不知道子究竟有多少。
Now, as buttons are dynamically created so I am confuse how to write code under button_click event as I dont know how many subcategories are there.
那么,有什么办法可以执行特定的按钮点击事件,这样我就可以执行某些命令?
So is there any way I can execute click event of a particular button , so that I can execute certain commands?
EDITED
这是code,我试图
Button btnDynamicButton = new Button();
private void btnclick_Click(object sender, EventArgs e)
{
label2.Text = btnDynamicButton.Text;
}
private void btnappetizer_Click(object sender, EventArgs e)
{
groupBox2.Visible =false;
DataTable dt = new DataTable();
dt = itemmasterbl.SelectallrecordFromtblItem(btnappetizer.Text);
for (int i = 0; i < dt.Rows.Count; i++)
{
string name = "Appetizer" + DynamicButtonCount;
Button btnDynamicButton1 = new Button();
btnDynamicButton1.Name = name;
btnDynamicButton1.Text = name;
btnDynamicButton1.Size =
new System.Drawing.Size(150, 30);
btnDynamicButton1.Location =
new System.Drawing.Point(180, DynamicButtonCount * 30);
btnDynamicButton1.Click +=new EventHandler(btnclick_Click);<br>
Controls.Add(btnDynamicButton1);
DynamicButtonCount++;
btnDynamicButton = btnDynamicButton1;
}
}
一旦我做到这一点,根据下开胃在itemmaster DB值的数量创建三个按钮,但一旦我点击任何三个按钮标签只显示最后一个按钮的文本,因为在最后一行我有:
Once I do this it creates three buttons according to number of values in itemmaster DB under appetizer, but once I click on any of the three buttons the label displays only last buttons text,because in last line I have :
btnDynamicButton = btnDynamicButton1;
btnDynamicButton = btnDynamicButton1;
这将持续按钮的相关信息,而是我想这永远按钮I preSS,标签应显示相应文本。我怎样才能做到这一点。
Which will last buttons infos,but rather I want which ever button I press, label should display respective text. How can I achieve this.
推荐答案
您可以将所有的逻辑到一个处理程序:
you can put all your logic into one handler:
System.Windows.Forms.Button b = new System.Windows.Forms.Button();
b.Click += new EventHandler(b_Click);
//finally insert the button where it needs to be inserted.
...
void b_Click(object sender, EventArgs e)
{
MessageBox.Show(((System.Windows.Forms.Button)sender).Name + " clicked");
}
要你的编辑:
您为您的字段 btnDynamicButton
内按钮(S)存储参考。因此,它总是被你所创建的最新按钮覆盖。你应该的不的使用领域引用按钮。点击处理程序的发件人
参数包含已单击按钮元素。见code以上:简单的投发件人
到按钮
,你知道被点击了哪个按钮:
You are storing the reference for your button(s) inside the Field btnDynamicButton
. Hence it always gets overwritten with the latest button you have created. You should not reference the button by using a field. The sender
parameter of the click-handler contains the button element that has been clicked. See the code above: Simple cast sender
to Button
and you know which button has been clicked:
private void btnclick_Click(object sender, EventArgs e)
{
Button btn = (Button)sender
label2.Text = btn.Text;
}
这篇关于如何在C#.NET在动态创建的按钮执行单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!