Re: [問題] for 迴圈 的Label (動態)

看板C_Sharp作者 (李崩子)時間10年前 (2014/06/04 18:17), 編輯推噓1(106)
留言7則, 3人參與, 最新討論串4/5 (看更多)
您好,方向是這邊沒錯!先謝謝你~ 但是我的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++; } } 執行前後變成:http://ppt.cc/rLdA 所以假設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++; : : } : : ....硬幹法 -- -- ▂▂ ▂▂▂ ▂▄▅▅▄▆█ _█◣ ◢ (●) ◣/\\ 丫 │◥  ̄◤ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 115.80.118.113 ※ 文章網址: http://www.ptt.cc/bbs/C_Sharp/M.1401877038.A.6FE.html

06/04 18:23, , 1F
可是我移植到Web版就不行了 囧
06/04 18:23, 1F

06/04 18:34, , 2F
06/04 18:34, 2F

06/04 19:39, , 3F
.NAME換成.ID 試試
06/04 19:39, 3F

06/04 21:40, , 4F
你想要的是搜尋到幾個東西,就把前幾個 label 填上字嗎?
06/04 21:40, 4F

06/04 21:41, , 5F
你應該先建立存取label的方法,最簡單情況下就先用array放
06/04 21:41, 5F

06/04 21:42, , 6F
把全部label都放進array,之後是對那個array做操作
06/04 21:42, 6F

06/05 07:12, , 7F
沒錯!!
06/05 07:12, 7F
文章代碼(AID): #1JZl8kR- (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #1JZl8kR- (C_Sharp)