从事件处理程序获取表单中的动态控件

编程入门 行业动态 更新时间:2024-10-28 10:28:08
本文介绍了从事件处理程序获取表单中的动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题是如何在下面的代码中使用btncki_click中的txtcki文本:

the problem is how can i use txtcki text in btncki_click in below code:

public Form2() { int xllngth,loc=20; for (; ; xllngth++) { var buncoockie = new System.Windows.Forms.Button(); buncoockie.Name = "btncki" + xllngth; buncoockie.Text ="cki"+ Convert.ToString(sheet1.Cells[xllngth, 2].value); buncoockie.Size = new Size(100, 20); buncoockie.Location = new System.Drawing.Point(145, loc); buncoockie.BackColor = Color.AliceBlue; buncoockie.Click += Btncki_Click; this.Controls.Add(buncoockie); var txtcki=new System.Windows.Forms.TextBox(); txtcki.Name = "txtcki" + xllngth; txtcki.Size = new Size(150, 20); txtcki.Location = new System.Drawing.Point(245, loc); this.Controls.Add(txtcki); loc += 20; } } private void Btncki_Click(object sender, EventArgs e) { //string temp=txtcki.text; }

推荐答案

首先,使用Button.Tag属性将它们绑定在一起: First, use the Button.Tag property to tie them together: var buncoockie = new System.Windows.Forms.Button(); buncoockie.Name = "btncki" + xllngth; buncoockie.Text = "cki" + Convert.ToString(sheet1.Cells[xllngth, 2].value); buncoockie.Size = new Size(100, 20); buncoockie.Location = new System.Drawing.Point(145, loc); buncoockie.BackColor = Color.AliceBlue; buncoockie.Click += Btncki_Click; var txtcki = new System.Windows.Forms.TextBox(); txtcki.Name = "txtcki" + xllngth; txtcki.Size = new Size(150, 20); txtcki.Location = new System.Drawing.Point(245, loc); buncoockie.Tag = txtcki; this.Controls.Add(buncoockie); this.Controls.Add(txtcki); loc += 20;

然后,在事件处理程序中:

Then, in the event handler:

private void Btncki_Click(object sender, EventArgs e) { Button buncoockie = sender as Button; if (buncoockie != null) { TextBox txtcki = buncoockie.Tag as TextBox; if (txtcki != null) { string temp = txtcki.text; ... } } }

更多推荐

从事件处理程序获取表单中的动态控件

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

发布评论

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

>www.elefans.com

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