Re: [问题] for 循环 的Label (动态)

楼主: sqrt1089 (李崩子)   2014-06-04 18:17:12
您好,方向是这边没错!先谢谢你~
但是我的for 是元素 (这边是未知)
以下是用你的code改过
private void button1_Click(object sender, EventArgs e)
{
string[] array2 = new string[] { "A", "B", "C", "D", "E" };
//^^ 这个矩阵是未知的,我先假设有ABCDE
int j = 1;
foreach (string str in array2)//
{
foreach (Control childc in this.Controls)
{
if (childc is Label)
{
if (childc.Name.ToString() !=
string.Format("label{0}",Convert.ToString(j).Trim())) continue;
childc.Text = str;
}
}
j++;
}
}
执行前后变成:

所以假设array2有N个元素(小于12),相对应的label 也会被取代^^
※ 引述《aacced (aacced)》之铭言:
: 不知道是不是你要的
: private void button1_Click(object sender, EventArgs e)
: {
: foreach (Control childc in this.Controls)
: {
: if (childc is Label)
: {
: if (childc.Name.ToString() != string.Format("label{0}", textBox1.Text.Trim())) continue;
: childc.Text = textBox2.Text;
: }
: }
: }
: 附上程式码:http://cht.tw/h/dvn3l
: ※ 引述《sqrt1089 (李崩子)》之铭言:
: : 暂时先解决问题,若有更好的方法会在更新
: : int ElementIndex = 1;
: : foreach (string element in Array)
: : {
: : if (ElementIndex == 1) { Label1.Text = element; }
: : else if (ElementIndex == 2) { Label2.Text = element; }
: : else if (ElementIndex == 3) { Label3.Text = element; }
: : else if (ElementIndex == 4) { Label4.Text = element; }
: : else if (ElementIndex == 5) { Label5.Text = element; }
: : else if (ElementIndex == 6) { Label6.Text = element; }
: : else if (ElementIndex == 7) { Label7.Text = element; }
: : else if (ElementIndex == 8) { Label8.Text = element; }
: : else if (ElementIndex == 9) { Label9.Text = element; }
: : else if (ElementIndex == 10) { Label10.Text = element; }
: : else if (ElementIndex == 11) { Label11.Text = element; }
: : else if (ElementIndex == 12) { Label12.Text = element; }
: : ElementIndex++;
: : }
: : ....硬干法
楼主: sqrt1089 (李崩子)   2014-06-04 18:23:00
可是我移植到Web版就不行了 囧图 " target="_blank" rel="nofollow">
作者: GoalBased (Artificail Intelligence)   2014-06-04 19:39:00
.NAME换成.ID 试试
作者: iterator (rotareti)   2014-06-04 21:40:00
你想要的是搜寻到几个东西,就把前几个 label 填上字吗?你应该先建立存取label的方法,最简单情况下就先用array放把全部label都放进array,之后是对那个array做操作
楼主: sqrt1089 (李崩子)   2014-06-05 07:12:00
没错!!

Links booklink

Contact Us: admin [ a t ] ucptt.com