Re: [問題] winform切換後picturebox消失的問題
※ 引述《wandallin (萬大林)》之銘言:
: 目前是要實做一個功能
: 就是按下按鈕後會跳出視窗, 把圖片放大到全畫面
: 程式大概是這樣
: Form1
: ShowPic ShowPicWindow = new ShowPic();
: ShowPicWindow.pb14 = pictureBox14;
: ShowPicWindow.ShowDialog();
: Form2
: public PictureBox pb14;
: this.Controls.Add(pb14);
: 可是這樣在Form1的picturebox會消失不見
: 這是什麼原因呢~"~???
根據 MSDN 對於 Control.ControlCollection.Add Method 的說明,
其中的 Note 提到:
A Control can only be assigned to one Control.ControlCollection at a time.
If the Control is already a child of another control it is removed from
that control before it is added to another control.
也就是說,控制項同一時間只能從屬於一個 Controls 集合物件,在您加入
Form1 的控制項到 Form2 的 Controls 時,該物件的 reference 會自動從
Form1 的 Controls 中移除。
一段小實驗:
1. 新增一個專案,在 Form1 上放置 PictureBox, GroupBox 與 Button 各一。
2. 指定 PictureBox 的 Image,隨便選一張圖片。
3. Button1's 的 Click 事件處理程序:
groupBox1.Controls.Add(pictureBox1);
4. 程式執行時,點一下按鈕,圖片會「變」到 GroupBox 中。
《解法》
Button1's 的 Click 事件處理程序:
PictureBox pb = new PictureBox();
pb.Image = pictureBox1.Image;
groupBox1.Controls.Add(pb);
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.41.99.30
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章