如何动态创建按钮单击事件? [英] How can create button click event dynamically?
本文介绍了如何动态创建按钮单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码在C#.net Windows应用程序中动态创建一个文本框和按钮.
Hi,
I create a text box and button dynamically in C# .net windows application by using following code.
private void lblNewUser_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//UserName
Label lblUN = new Label();
lblUN.Text = "UserName";
lblUN.Font = new Font(lblUN.Font, lblUN.Font.Style | FontStyle.Bold);
lblUN.BackColor = Color.Transparent;
lblUN.ForeColor = Color.White;
lblUN.AutoSize=true;
lblUN.Location = new Point(30,300);
this.Controls.Add(lblUN);
//Password
Label lblPWD = new Label();
lblPWD.Text = "Password";
lblPWD.Font = new Font(lblPWD.Font,lblPWD.Font.Style|FontStyle.Bold);
lblPWD.BackColor = Color.Transparent;
lblPWD.ForeColor = Color.White;
lblPWD.AutoSize = true;
lblPWD.Location = new Point(30,350);
this.Controls.Add(lblPWD);
// Save Button
Button btnSAVE = new Button();
btnSAVE.Text="Save";
btnSAVE.Font = new Font(btnSAVE.Font,btnSAVE.Font.Style|FontStyle.Bold);
btnSAVE.Size = new Size(50,25);
btnSAVE.BackColor = Color.Blue;
btnSAVE.ForeColor = Color.White;
btnSAVE.Location = new Point(100, 450);
this.Controls.Add(btnSAVE);
}
现在,当我按下保存"按钮时,我想将在该文本框中输入的值存储到数据库中.请给我答复.在哪里使用以下代码.
Now when I press this Save button,I want to store the value which is entered in that text box into database. Kindly send me answer.Where shouid I use the following codes.
btnobj1.Click+=new System.EventHandler(ButtonClickHandler);
private void ButtonClickHandler(object sender, EventArgs e) {
Button button = sender as Button;
if (button.Text == "Save") {
//Do whatever
}
}
谢谢,
Viswanathan.M
代码在"pre"标记中被阻止[/编辑]
thanks,
Viswanathan.M
Code is blocked in "pre" tags[/Edited]
推荐答案
btnobj1.Click+=new System.EventHandler(ButtonClickHandler);
private void ButtonClickHandler(object sender, EventArgs e) {
Button button = sender as Button;
if (button.Text == "Save") {
//Do whatever
}
}
检查此链接.
http://www.dotnetspider.com/resources/26678- How-add-click-event-for-dynamically-generated.aspx [
Check this link.
http://www.dotnetspider.com/resources/26678-How-add-click-event-for-dynamically-generated.aspx[^]
您需要为按钮OnClick事件创建事件处理程序.
you need to create the eventhandler for the button OnClick event.
btnobj1.Click += new EventHandler(btnobj1_Click);
你应该有方法
and you should have the method
private void btnobj1_Click(Object sender, EventArgs e)
{
// here you can play with the txtobj3.Text
}
这篇关于如何动态创建按钮单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文