日期:2014-05-18 浏览次数:20828 次
private void button1_Click(object sender, EventArgs e) { string[] xxy = { "txtsjdbdh", "txtxmjldh" }; foreach (string i in xxy) { Control[] cs = this.Controls.Find(i, true); if (cs != null && cs.Length > 0) { TextBox tb = cs[0] as TextBox; if (tb != null) { tb.Text = i; } } } }
------解决方案--------------------
字符串有text属性呢?
string[] xxy = { "txtsjdbdh", "txtxmjldh" };///这是textbox的name集合?
假如是的话
foreach (string i in xxy)
{
////这里面你应该通过i 去找你页面中的textbox 找到后在 转换为textbox 然后在给属性赋值.
而不是想你这样
i.text = "sfds";
}
还有你都知道TextBox的name了 干嘛不直接txtsjdbdh.text="00000000";呢
或者
list<objct> textobj=new list<objct>()
textobj.add(txtsjdbdh)
textobj.add(txtxmjldh)
foreach (objct text in textobj)
{
textbox test=text as textbox;
if(test is textbox)
{
test.text = "sfds";
}
}
------解决方案--------------------
数组里面存放两个textbox控件名称
string[] xxy = { "txtsjdbdh", "txtxmjldh" };
foreach (textbox i in xxy)
{
i.text = "sfds";
}
------解决方案--------------------
另外textbox命名规则最好写成 txt_sjdbdh,txt_xmjldh
这样比较美观,也比较看懂
------解决方案--------------------
foreach (Control c in this.Controls) { if (c.GetType() == typeof(TextBox)) { TextBox t = c as TextBox; t.Text = "fdsaf"; }
------解决方案--------------------
List<TextBox> txtList = new List<TextBox>();
public Form25()
{
InitializeComponent();
txtList.Add(textBox1);
txtList.Add(textBox2);
foreach (TextBox tb in txtList)
{
tb.Text = "fdsaf";
}
}