private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 50; i++)
{
TextBox txtBox = new TextBox();
txtBox.Name = "TextBox" + i.ToString();
txtBox.Width = 50;
txtBox.Location = ...
txtBox.Text=""+i;//初始值
this.Controls.Add(txtBox);
}
}
另外,我在FORM上添加了一个按扭,在用户点击后,获取将生成的textbox的值,并且给每个textbox更新数值.
但由于自己是学C#不久,这方面的内容了解不多,希望大家给提示,怎样获取生成的textbox的值,以及怎样修改每个textbox的值??
5 个解决方案
#1
重写 OnInit 方法
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
for (int i = 0; i < 50; i++)
{
TextBox txtBox = new TextBox();
txtBox.ID= "TextBox" + i.ToString(); //使用id
txtBox.Width = 50;
// txtBox.Location = ...
txtBox.Text=""+i;//初始值
this.Controls.Add(txtBox);
}
}
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 50; i++)
{
string id="TextBox" + i.ToString();
//循环获取值
string value=((TextBox)this.FindControl(id)).Text;
}
}
上面是用aspx实现的.和c#原来是一样.改一改就可以了.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
for (int i = 0; i < 50; i++)
{
TextBox txtBox = new TextBox();
txtBox.ID= "TextBox" + i.ToString(); //使用id
txtBox.Width = 50;
// txtBox.Location = ...
txtBox.Text=""+i;//初始值
this.Controls.Add(txtBox);
}
}
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 50; i++)
{
string id="TextBox" + i.ToString();
//循环获取值
string value=((TextBox)this.FindControl(id)).Text;
}
}
上面是用aspx实现的.和c#原来是一样.改一改就可以了.
#2
遍历this.Controls,看Name和你要找的一样就可以了
#3
不对啊,c#里面没有这个函数,this.FindControl(),二楼能说清楚点不?
#4
Sorry 理解错了.
public Form1()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
TextBox txtBox = new TextBox();
txtBox.Name = "TextBox" + i.ToString();
txtBox.Width = 50;
txtBox.Location =new System.Drawing.Point(i*20, i*20);
txtBox.Text=""+i;//初始值
this.Controls.Add(txtBox);
}
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++)
{
list.Add("TextBox" + i.ToString());
}
foreach (Control z in Controls)
{
if (list.Contains(z.Name))
{
string val = z.Text;//获取每个控件的值
}
}
}
public Form1()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
TextBox txtBox = new TextBox();
txtBox.Name = "TextBox" + i.ToString();
txtBox.Width = 50;
txtBox.Location =new System.Drawing.Point(i*20, i*20);
txtBox.Text=""+i;//初始值
this.Controls.Add(txtBox);
}
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++)
{
list.Add("TextBox" + i.ToString());
}
foreach (Control z in Controls)
{
if (list.Contains(z.Name))
{
string val = z.Text;//获取每个控件的值
}
}
}
#5
没搞明白
#1
重写 OnInit 方法
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
for (int i = 0; i < 50; i++)
{
TextBox txtBox = new TextBox();
txtBox.ID= "TextBox" + i.ToString(); //使用id
txtBox.Width = 50;
// txtBox.Location = ...
txtBox.Text=""+i;//初始值
this.Controls.Add(txtBox);
}
}
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 50; i++)
{
string id="TextBox" + i.ToString();
//循环获取值
string value=((TextBox)this.FindControl(id)).Text;
}
}
上面是用aspx实现的.和c#原来是一样.改一改就可以了.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
for (int i = 0; i < 50; i++)
{
TextBox txtBox = new TextBox();
txtBox.ID= "TextBox" + i.ToString(); //使用id
txtBox.Width = 50;
// txtBox.Location = ...
txtBox.Text=""+i;//初始值
this.Controls.Add(txtBox);
}
}
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 50; i++)
{
string id="TextBox" + i.ToString();
//循环获取值
string value=((TextBox)this.FindControl(id)).Text;
}
}
上面是用aspx实现的.和c#原来是一样.改一改就可以了.
#2
遍历this.Controls,看Name和你要找的一样就可以了
#3
不对啊,c#里面没有这个函数,this.FindControl(),二楼能说清楚点不?
#4
Sorry 理解错了.
public Form1()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
TextBox txtBox = new TextBox();
txtBox.Name = "TextBox" + i.ToString();
txtBox.Width = 50;
txtBox.Location =new System.Drawing.Point(i*20, i*20);
txtBox.Text=""+i;//初始值
this.Controls.Add(txtBox);
}
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++)
{
list.Add("TextBox" + i.ToString());
}
foreach (Control z in Controls)
{
if (list.Contains(z.Name))
{
string val = z.Text;//获取每个控件的值
}
}
}
public Form1()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
TextBox txtBox = new TextBox();
txtBox.Name = "TextBox" + i.ToString();
txtBox.Width = 50;
txtBox.Location =new System.Drawing.Point(i*20, i*20);
txtBox.Text=""+i;//初始值
this.Controls.Add(txtBox);
}
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++)
{
list.Add("TextBox" + i.ToString());
}
foreach (Control z in Controls)
{
if (list.Contains(z.Name))
{
string val = z.Text;//获取每个控件的值
}
}
}
#5
没搞明白