2012-10-08 175 views
0

我無法將我的第二個窗體中的圖像PictureBox轉換爲已打開的第一個窗體。將圖像從Form2傳遞到Form1

我使用這個代碼:

private void btnCancel_Click(object sender, EventArgs e) 
{ 
    var frmAdd = new frmAdd(); 

    frmAdd.pctImage.Image = pctImage.Image; 

    if (btnCancel.Text == "C&OPY") 
    { 
     this.Close(); 
     return; 
    } 
    if (btnCancel.Text == "C&ANCEL") 
    { 
     this.Close(); 
     return; 
    } 
} 

我希望有人能夠幫助我。謝謝。

回答

1

根據documentation,您應該在將圖像從一種形式傳遞給另一種形式時創建圖像的克隆。喜歡的東西:

frmAdd.pctImage.Image = pctImage.Image.Clone() as Image; 

編輯:作爲lumberjack4指出的那樣,你還可以創建一個新的,看不見的frmAdd和圖像分配給這種形式,而不是已經所示。圖像可能實際上被正確分配(儘管您仍然應該克隆它),但它永遠不會在屏幕上顯示,因爲您的本地frmAdd從不顯示。下面是一些代碼,會告訴你如何做到這一點:

frmAdd ---------:

public partial class frmAdd : Form 
{ 
    // Stores a reference to the currently shown frmAdd instance. 
    private static frmAdd s_oInstance = null; 

    // Returns the reference to the currently shown frmAdd instance 
    // or null if frmAdd is not shown. Static, so other forms can 
    // access this, even when an instance is not available. 
    public static frmAdd Instance 
    { 
     get 
     { 
      return (s_oInstance); 
     } 
    } 

    public frmAdd() 
    { 
     InitializeComponent(); 
    } 

    // Sets the specified picture. This is necessary because picAdd 
    // is private and it's not a good idea to make it public. 
    public void SetPicture (Image oImage) 
    { 
     picAdd.Image = oImage; 
    } 

    // These event handlers are used to track if an frmAdd instance 
    // is available. If yes, they update the private static reference 
    // so that the instance is available to other forms. 
    private void frmAdd_Load (object sender, EventArgs e) 
    { 
     // Form is loaded (not necessarily visible). 
     s_oInstance = this; 
    } 

    private void frmAdd_FormClosed (object sender, FormClosedEventArgs e) 
    { 
     // Form has been closed. 
     s_oInstance = null; 
    } 

    // Whatever other code you need 
} 

在frmNew ---------:

public partial class frmNew : Form 
{ 
    public frmNew() 
    { 
     InitializeComponent(); 
    } 

    private void btnCancel_Click (object sender, EventArgs e) 
    { 
     // Is there an active instance of frmAdd? If yes, 
     // call SetPicture() with a copy of the image used by 
     // this frmNew. 
     frmAdd oFrm = frmAdd.Instance; 

     if (oFrm != null) 
      oFrm.SetPicture (picNew.Image.Clone() as Image); 
    } 
} 

有2 PictureBox涉及對照:picAddfrmAddpicNewfrmNew。當點擊btnCancel時,frmNew中的代碼將檢查是否存在有效的frmAdd實例,如果是,則設置其圖像。

請注意picAdd不是公共控件 - 它應該是私人的。將控件設置爲公開形式並不是一個好主意,因爲它允許不受控制地訪問它們,並且表單不會確切地知道它們的狀態(因爲其他人可能會在沒有表單知道的情況下更改這些控件)。這可能導致變化很難在更大的程序中修復錯誤。

如果您需要訪問其窗體之外的控件,請將控件保留爲私有,並在需要時更新控件的形式中創建公共屬性/方法 - 如上面的SetPicture方法。這仍然可以讓你從表單之外分配圖片,但是表單控制了這種情況的發生,因爲SetPicture可以驗證圖像等。如果你只是將你的控件設置爲公開,這是不可能的。

+0

仍然沒有工作... :( 我已經試過這個。 –

+0

當你發生什麼事情 嘗試這個?你有沒有發現異常或者什麼都沒有顯示? – xxbbcc

+0

只是沒有顯示.. –

3
var frmAdd = new frmAdd(); 

此行看起來像您正在創建第一個表格frmAdd的新實例。因此,不是將圖像傳遞給打開的窗體,而是將其傳遞給另一個不同的實例。

如果您的第二個表單是由frmAdd創建的,則可以將屬性添加到第二個表單,該表單引用frmAdd,然後使用該屬性設置圖像。

你可以有一些看起來像這樣的:

1表格

// Just a way to launch the 2nd form 
private void LaunchPictureForm() 
{ 
    frmPicture pictureForm = new frmPicture(); 
    pictureForm.MyFirstForm = this; 
    pictureForm.Show(); 
} 

第二表格

public frmAdd MyFirstForm { get; set; } 

private void btnCancel_Click(object sender, EventArgs e) 
{ 
    MyFirstForm.pctImage.Image = pctImage.Image; 
} 
+0

我得到'NullReference Exception is unhandled' 未將對象引用設置爲對象的實例。 –

+0

您需要在創建第二個表單時設置參考。看我的編輯。 – lumberjack4