2012-10-08 102 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