看板 C_Sharp 關於我們 聯絡資訊
小弟做了一些練習 動態物件產生與事件都正常,唯有下面這個問題. ------ 我有UserControl, 簡稱UC, 裡面有label1 在form1內用for迴圈產生2個UC1,UC2 問題來了, 我要怎麼寫入或取出 UC1的label1 和 U2的label1的text呀?^^ ------ 下面是我的code public Form1() { InitializeComponent(); for (int i = 0; i <= 1; i++) { UC1 UC = new UC1(); UC.Location = new System.Drawing.Point(40, 60*i); UC.Name = "UC" + i.ToString(); Controls.Add(UC); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.171.12.58 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1566637727.A.590.html
testPtt: 預設存取為private 改public就行 08/24 17:35
GooLoo: 剛看UC 預設是public 08/24 19:48
GooLoo: 弄出來了,感謝 08/24 21:36
GooLoo: 發現我的問題主要是button1內new動態物件,button2內看不 08/25 09:24
GooLoo: 到它,new物件是區域的,不曉得怎麼改成全域。 08/25 09:24
kobe8112: 宣告在大家都看的到的地方啊 08/25 21:14
testPtt: 猜你是要foreach(control c in form1)if(c is UC)... 08/26 08:29
feobzq: 在class宣告全域的UC1 UC; for裡面改UC = new UC1(); 08/26 13:27
annies5: 不一定要宣告全域,但一定要知道父項(Parent)是誰 08/28 14:50
Litfal: 正確的方法是在UC裡面開屬性存取吧 08/28 17:26
kobe8112: 即使有get, set,你連物件參考都拿不到也沒法存取啊... 08/28 20:48
wl00827438: UC1.controls["label1"].text? 08/30 09:35