[問題] 修改動態textbox的Text屬性

看板C_Sharp (C#)作者 (suxian)時間7年前 (2017/12/27 23:53), 編輯推噓1(104)
留言5則, 3人參與, 8年前最新討論串1/1
各位大大好 小弟是剛入門C#的菜鳥 最近在開發一個專案 使用的是visual studio 2017 以下是我的程式碼概述 namespace WindowsFormsApp1 { public partial class Form1 : Form { TextBox test = new TextBox(); public Form1() { InitializeComponent(); for (int i = 0; i < 10; i++) { this.test = new TextBox(); test.Name = "test" + Convert.ToString(i); test.Location = new Point(0, 20 + i * 20); test.Text = Convert.ToString(i); tabPage1.Controls.Add(test); } } private void button1_Click(object sender, EventArgs e) { } } } 一開始使用FOR迴圈創10個TextBox 而且皆有預設值 現在想要用一個Button 按下後 把另一個陣列裡的所有值取代每一個TextBox.Text 目前碰到的問題是 (1)該如何去抓取每一個TextBox的Name屬性進而去更改Text屬性 (2)TextBox 的Name該如何轉換 我的每一個TextBox是"test" + Convert.ToString(i) 在抓取的時候是要用 ("test" + Convert.ToString(i)).Name嗎? 看過版上大致上的方法都是用FindControl 可是我的visual studio 2017並沒有這個方法 是因為版本不同所致的嗎?或是有另外的方法可以達到我要的效果? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.73.161.11 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1514390005.A.053.html

12/28 01:38, 7年前 , 1F
textbox new完後找個陣列放著
12/28 01:38, 1F

12/28 08:44, 7年前 , 2F
tabPage1.Controls不就是一個Collection嗎?
12/28 08:44, 2F

03/02 16:43, 8年前 , 3F
(TextBox)this.Controls.Find("test"+Con...)[0]
03/02 16:43, 3F

03/02 16:45, 8年前 , 4F
test的宣告改為private System.Windows.Forms.Text test;
03/02 16:45, 4F

03/02 16:47, 8年前 , 5F
private System.Wiundows.Forms.TextBox test;
03/02 16:47, 5F
文章代碼(AID): #1QGy7r1J (C_Sharp)
文章代碼(AID): #1QGy7r1J (C_Sharp)