如何动态创建按钮单击事件?

编程入门 行业动态 更新时间:2024-10-17 21:15:18
本文介绍了如何动态创建按钮单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用以下代码在C# Windows应用程序中动态创建一个文本框和按钮.

Hi, I create a text box and button dynamically in C# 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 } }

检查此链接. www.dotnetspider/resources/26678- How-add-click-event-for-dynamically-generated.aspx [ ^ ] Check this link. www.dotnetspider/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 }

更多推荐

如何动态创建按钮单击事件?

本文发布于:2023-11-02 14:20:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   事件   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!