首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

TextBox属性不知怎么加值进去,请大家帮帮

2012-09-07 
TextBox属性不知如何加值进去,请大家帮帮string[] xxy { txtsjdbdh, txtxmjldh }foreach (string i

TextBox属性不知如何加值进去,请大家帮帮
string[] xxy = { "txtsjdbdh", "txtxmjldh" };
foreach (string i in xxy) 
{
  i.text = "sfds";
}


i.text应如何写呀

[解决办法]

C# code
  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
这样比较美观,也比较看懂
[解决办法]
C# code
 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";
}
}

热点排行