-1

How can I change the image of a form PictureBox, from another form? Example. Form2 image needs to change Form1 image.

I tried this.

    exmp.Code 1        

    //This is for Form1
    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        Form2 Fr2 = new Form2();
        Fr2.Owner = this;
    }

        private void pictureBox2_Click(object sender, EventArgs e)
    {
        Fr2.Show();
    }

    //This is for Form2
        private void pictureBox2_Click(object sender, EventArgs e)
    {
        try
        {
            (this.Owner as Form1).pictureBox1.Image = pictureBox2.Image;
        }
        catch
        {

        }
    }

This code doesn't work! But if I try this it will work.

    exmp.Code 1        

    //Form1
        private void pictureBox1_Click(object sender, EventArgs e)
    {
        Form2 Fr2 = new Form2();
        Fr2.Show();
        Fr2.Owner = this;
    }
    
    //Form2 
    private void pictureBox2_Click(object sender, EventArgs e)
    {
        try
        {
            (this.Owner as Form1).pictureBox1.Image = pictureBox2.Image;
        }
        catch
        {

        }
    }

But I cannot use the second code because I need to click a ContextMenuStrip to open Form2.

Any question write in comments!

Detroit
  • 11
  • 5
  • Does this answer your question? [Interaction between forms — How to change a control of a form from another form?](https://stackoverflow.com/questions/38768737/interaction-between-forms-how-to-change-a-control-of-a-form-from-another-form) –  Dec 09 '20 at 19:26
  • Also this duplicate: [How to access a form control for another form?](https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) –  Dec 09 '20 at 19:27
  • It's very different from PictureBox and images, I know how to do it with values, but I'm not sure I know with images. If you could answer this question with a solution it would be helpful! – Detroit Dec 09 '20 at 19:31
  • A picture box is a specialized control. All patterns for accessing a control of another form from a form work, depending on the scenario and behavior. –  Dec 09 '20 at 19:35
  • A "*wild*" solution is to make forms as singleton. Personally, I create all my single instance forms as singleton so that I can do whatever I want, espacially with the form having a typed DataSet instance (there is no data module like in Delphi). Only temporary forms are conventional. –  Dec 09 '20 at 19:38

1 Answers1

1

In Form1, "Fr2" needs to be declared at Form level so you can access it from both "pictureBox1_MouseEnter" and "pictureBox2_Click". Set the Owner by passing "this" to Show().

This is for Form1:

private Form2 Fr2;

private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
    Fr2 = new Form2();
}

private void pictureBox2_Click(object sender, EventArgs e)
{
    if (Fr2!=null && !Fr2.IsDisposed)
    {
        Fr2.Show(this); // <-- pass Form1 as the Owner
    }
}

This is for Form2:

private void pictureBox2_Click(object sender, EventArgs e)
{
    Form1 Fr1 = this.Owner as Form1;
    if (Fr1!=null)
    {
        Fr1.pictureBox1.Image = pictureBox2.Image;
    }
}

To access "pictureBox1" in Form1, though, you'd have to change the Modifiers property of it to public.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40