//} //方法二: 查找所有控件 for (int i = 1; i <11; i++) { string _box = "textBox" + i.ToString(); if (this.findControl(this, _box) != null) { TextBox tb = (TextBox)this.findControl(this, _box); tb.Text =temp[i-1]; } } } //<summary> /// 在winform中查找控件 ///</summary> ///<param ></param> ///<param ></param> ///<returns></returns> private System.Windows.Forms.Control findControl(System.Windows.Forms.Control control, string controlName) { System.Windows.Forms.Control c1; foreach (System.Windows.Forms.Control c in control.Controls) { if (c.Name == controlName) { return c; } else if (c.Controls.Count > 0) { c1 = findControl(c, controlName); if (c1 != null) { return c1; } } } return null; }
for(int i = 0;i < temp.Length;i++) xxx.Controls["textBox" + (i+1)].Text=temp[i]
用BinaryWriter封装成字节。 byte[] buffer = null; using (MemoryStream mem = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(mem); writer.Write(12); writer.Write(13.1f); writer.Write('c'); buffer = mem.ToArray(); mem.Close(); }
var temp = new string[100]; Enumerable.Range(1, temp.Count()) .Select(x => { var control = this.Controls.Find("textbox" + x) as TextBox; if (control != null) control.Text = temp[x - 1]; });
textbox1.text 了
int i= 1;
while (i < 4)
{
string str = string.Format("textBox{0}",i);
FieldInfo frild = this.GetType().GetField(str, BindingFlags.Instance | BindingFlags.NonPublic);
frild.FieldType.GetProperty("Text").SetValue(frild.GetValue(this), i.ToString(), null);
i++;
}
不要手动建立100个TextBox对象,太累
String[] temp = new String[10] { "str0", "str1", "str2", "str3", "str4", "str5", "str6", "str7", "str8", "str9" };
//方法一: 利用tabIndex 特点简单快捷 但是功能简单
//foreach (TextBox control in this.panel1.Controls)
//{
// control.Text = temp[control.TabIndex];
//}
//方法二: 查找所有控件
for (int i = 1; i <11; i++)
{
string _box = "textBox" + i.ToString();
if (this.findControl(this, _box) != null)
{
TextBox tb = (TextBox)this.findControl(this, _box);
tb.Text =temp[i-1];
}
}
}
//<summary>
/// 在winform中查找控件
///</summary>
///<param ></param>
///<param ></param>
///<returns></returns>
private System.Windows.Forms.Control findControl(System.Windows.Forms.Control control, string controlName)
{
System.Windows.Forms.Control c1;
foreach (System.Windows.Forms.Control c in control.Controls)
{
if (c.Name == controlName)
{
return c;
}
else if (c.Controls.Count > 0)
{
c1 = findControl(c, controlName);
if (c1 != null)
{
return c1;
}
}
}
return null;
}
using (MemoryStream mem = new MemoryStream())
{
BinaryWriter writer = new BinaryWriter(mem);
writer.Write(12);
writer.Write(13.1f);
writer.Write('c');
buffer = mem.ToArray();
mem.Close();
}
Enumerable.Range(1, temp.Count())
.Select(x =>
{
var control = this.Controls.Find("textbox" + x) as TextBox;
if (control != null) control.Text = temp[x - 1];
});