从form2更改表单1中的图片框的图像

时间:2014-06-06 08:14:57

标签: c# forms picturebox

我制作了一个程序,我需要在Form1中更改我的Picturebox。这需要以第二种形式(Form2)

进行

窗体2:

private void btnBrowseFirstAd_Click(object sender, EventArgs e)
    {

        OpenFileDialog OFD = new OpenFileDialog();

        if (OFD.ShowDialog() == DialogResult.OK)
        {
            tbFirstAdBlock.Text = OFD.FileName;
        }
    }

    private void btnChangeFirstAd_Click(object sender, EventArgs e)
    {           

        Form1 Screen = new Form1();

        Screen.changePB(tbFirstAdBlock.Text);
    }

Form1中

    public void changePB(string PBLocation)
    {
        pbAddOne.Image = Image.FromFile(PBLocation);
    }

可悲的是,图片框并没有改变。

1 个答案:

答案 0 :(得分:0)

在Form2中,您需要链接到Form1:

class Form2
{
  public Form1 form1;
.....
 private void btnChangeFirstAd_Click(object sender, EventArgs e)
    {           
        form1.changePB(tbFirstAdBlock.Text);
        form1.Refresh();
    }
}

...

void Main()
    {
       Form1 Screen =new Form1();
       Form2 Screen1 =new Form2();
       Screen1.form1=Screen;
    }

...