如何在运行时创建多个文本框以及如何访问它? [英] How to create multiple textbox at runtime and how can access it?
问题描述
我想使用按钮单击事件在运行时创建多个文本框.以及如何获取文本框ID,即如何访问它?
我已实现在运行时创建多个文本框,但无法访问它.
这是在运行时创建多个文本框的代码...
I would like to create multiple textbox at runtime using a button click event. And how can i get the textboxes id i.e. how can i access it????
I have implemented to create a multiple textbox at runtime but i can''t access it.
Here is the code to create multiple textbox at runtime...
namespace multiple_textbox
{
public partial class Form1 : Form
{
int a = 0;
public Form1()
{
InitializeComponent();
}
private void btntxtbx_Click(object sender, EventArgs e)
{
a++;
int count = 0 + a ;
TextBox[] t = new TextBox[count];
int x = 70, y = 50;
for (int i = 0; i < count; i++)
{
t[i] = new TextBox();
if(this.Width!=1152)
{
t[i].Location = new System.Drawing.Point(y +(i*65), x);
t[i].Size = new Size(100, 50);
t[i].Name = "txtbx" + i.ToString();
this.Controls.Add(t[i]);
this.ResumeLayout(false);
y += 40;
this.Refresh();
}
}
}
}
}
推荐答案
设置
TextBox[] t
更大的范围.
在课程级别定义它.
bigger scope.
Define it in class level.
int a = 0;
TextBox[] t;
public Form1()
{
InitializeComponent();
}
private void btntxtbx_Click(object sender, EventArgs e)
{
a++;
int count = 0 + a;
t = new TextBox[count];
int x = 70, y = 50;
for (int i = 0; i < count; i++)
{
t[i] = new TextBox();
if (this.Width != 1152)
{
t[i].Location = new System.Drawing.Point(y + (i * 65), x);
t[i].Size = new Size(100, 50);
t[i].Name = "txtbx" + i.ToString();
this.Controls.Add(t[i]);
this.ResumeLayout(false);
y += 40;
this.Refresh();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (t == null)
return;
TextBox t2 = t.Where(o => o.Name == "txtbx0")
.FirstOrDefault() as TextBox;
Console.WriteLine("txtBox: "+t2.Name);
}
希望对您有帮助.
Hope this will help you.
设置每个texdtbox的ID
喜欢
Set the Id of each texdtbox
Like
t[i].ID = "TestBox" + i.ToString();
在按钮单击事件之外声明该文本框对象.
意思是:全球性
现在尝试使用其ID访问文本框.
一条建议:
您已经写了
declare that textbox object outside the button click event.
Means:Globaly
Now try to access the textbox using their ID.
One suggestion:
You have written that
int count = 0 + a;
我认为不需要写0 +瞬间,只需写
I think no need to write 0 + a instant of that simply write
int count = a;
首先,很可能您以后需要访问文本框.如何?只需使引用TextBox[] t
在您需要的生存期内即可访问.通常,您需要使此数组引用表单类的字段或属性. (仅给它一个好的语义名称;使用一个字符的标识符总是很不好的.)
您的代码有许多问题要解决.不好的事情是所有在代码中硬编码的立即常量:70、50、1142、65、100、50,"txtbx"…然后声明所有显式常量或放入资源中.对这些控件的父级使用带有停靠/填充的单独面板.一个明显的问题是尺寸.您永远不要分配高度;它应该保持默认值.一个明显的问题是您不知道索引的大小就按索引排列控件.您应该做一件非常不同的事情;更好地垂直排列它们并计算高度以确定垂直步长:
First of all, most likely you will need to access the text boxes later. How? Simply make the referenceTextBox[] t
accessible during the life time you need. Most usually, you would need to make this array reference a field or a property of your form class. (Only give it a good semantic name; using one-character identifiers is always bad.)
You code has a number of problems to fix. Bad things is all your immediate constants hard-coded in the code: 70, 50, 1142, 65, 100, 50, "txtbx"… Declare then all explicit constants or put in resources. Use a separate panel with docking/padding for a parent of these controls. One apparent problem is the size. You should never assign the height; it should remain default. One apparent problem that you arrange the controls by index without knowing its size. You should do a very different thing; better arrange them vertically and calculate the height to determine the vertical step:
int gap = 4;
for (int i = 0; i < count; i++) {
t[i] = new TextBox();
//if (this.Width != 1152) silly check, if will give you always true or always false
{
t[i].Location = new System.Drawing.Point(y * (t[i].Height + gap), x); //height it taken into account
//...
this.Controls.Add(t[i]);
}
}
最后,删除Refresh
和ResumeLayout
.有时您需要ResumeLayout
,但肯定不在循环之内.
Finally, remove Refresh
and ResumeLayout
. Sometimes you need ResumeLayout
, but certainly out of the loop.
这篇关于如何在运行时创建多个文本框以及如何访问它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!