hi everyone!!! need some help here, im having a hard time in passing an image to another form, i have two forms (form1 i have a picturebox, in form 2 i have a combobox, a picture box and a button), when i select from my combobox a picture will be displayed in the picturebox then when i terminate my form2, what i want to happen is to display in form1 picturebox what was displayed in my form2 picturebox. please help me...

Recommended Answers

All 2 Replies

Are you opening Form2 from Form1? Is Form2 opened modally (ShowDialog())?

If you aren't showing it modally then check out my tutorial for accessing controls across forms

If you are showing it modally then you can add a public property to Form2 that returns the image and use that from Form1 when Form2 is closed:

public partial class Form1 : Form
{
    private void ButtonClick (object sender, EventArgs e)
    {
        Form2 frm = new Form2();
        frm.ShowDialog(); //show modal form

        //get image when form is closed
        pictureBox.Image = frm.SelectedImage;
    }
}

public partial class Form2 : Form
{ 
    //read only property to return image
    public Image SelectedImage
    {
        get { return pictureBox.Image; }
    }
}

When a form is opened modally, the code execution pauses on the calling method until the form is closed. I would recommend setting the DialogResult of Form2 and checking it in Form1 to ensure an image was selected before retreiving it.

Code for form1

namespace form
{
    public partial class Form1 : Form
    {     
        public Form1()
        {
            InitializeComponent();
        }
        public Form1(System.Drawing.Image  i)
        {         InitializeComponent();
                  pictureBox1.Image = i;
        }
     }
}

Code for form2

namespace form
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1(pictureBox1.Image);
            f.Show();
        }
    }
}

In this code I create a parameterize constructor in form1

[B]
public Form1(System.Drawing.Image  i)
        {         InitializeComponent();
                  pictureBox1.Image = i;
        }[/B]

It take image which is pass by from2.
In form two I use click event to send image two form1 if want send image with another event simply copy and paste the code from click event to that event.
Here I am not show the code combobox.

Best Of Luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.