Re: [問題] for 迴圈 的Label (動態)
您好,以下我改成Web版,您參考參考!
protected void Button1_Click(object sender, EventArgs e)
{
string[] array2 = new string[] { "A", "B", "C", "D", "E" };
//^^ 這個矩陣是未知的,我先假設有ABCDE
for (int i = 0; i < array2.Length; i++)
{
Label myLabel = this.FindControl(string.Format("label{0}", (i+1).ToString())) as Label;
if (myLabel == null) continue;
myLabel.Text = array2[i];
}
}
※ 引述《sqrt1089 (李崩子)》之銘言:
: 您好,方向是這邊沒錯!先謝謝你~
: 但是我的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
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.255.142.109
※ 文章網址: http://www.ptt.cc/bbs/C_Sharp/M.1401894287.A.B7E.html
※ 編輯: aacced (111.255.142.109), 06/04/2014 23:14:43
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 5 之 5 篇):
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章