-3

I have two pictureboxes in two different forms.

  • Form1 : firstpicturebox
  • form2 : picturebox1

I want to transfer the image of firstpicturebox to picturebox1.

So, can anyone help and provide the solution for the same.

2 답변


1

You can send it using Form2 Constructor

Try This:

Form1:

Form2 form2 = null;

private void button1_Click(object sender, EventArgs e)
    {
        form2 = new Form2(pictureBox1.Image);
        form2.Show();
    }

Form 2:

public Form2(Image pic1)
    {
        InitializeComponent();
        pictureBox1.Image = pic1;
    }


  • Here, I am not using firstpicturebox of form1. - VGI
  • How will I access firstpicturebox within new form2()..? - VGI
  • I would highly recommend to keep the reference to Form2 as a class level variable in Form1 ! I have taken the liberty to edit the answer. - TaW

1

You are really asking two questions in one:

1 - How can I get the content of one PictureBox into another PictureBox?

2 - How can I access the controls etc. of one form from another form?

Question 1 is straightforward: pictureBox1.Image = pictureBox2.Image;

The answer to question 2 is not hard either but there are many ways to do it and selecting one may depend on what else you want to do with the two forms.

The basic way is always to get a valid reference to the other form.

Here is a general purpose method:

  • Declare global variables of the other form's class in each form.
  • Let these references point to the other form at the right moment!
  • Make any controls, properties etc. you want to access public; for controls you can either create an extra reference and fill it in or you can go to the designer and change its scope from private to public there.

What is the right moment? Assuming form1 is created at the programm's startup and form2 is created by some action in form1 you could get the reference to form2 right there when you create and show it:

    form2 = new Form2(this);
    form2.Show();

This could be in a button click or even in the load event of form1.

Note that I have handed a reference to this in the constructor! This is a nice way to pass the reference to form1 to the new form. Therefore the contructor in form2 should look like this:

public Form2(Form1 form1_)
{
    InitializeComponent();
    form1 = form1_;
}

The last step is to make the controls public which you need to access. Go to the Designer.cs file and change the declaration from

private System.Windows.Forms.PictureBox pictureBox1;

to

public System.Windows.Forms.PictureBox pictureBox1;

Done.

Or...

If you have many forms which all need to access the one PictureBox, you could also try this: Declare a static global reference to it in the programm.cs file like this:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

public static PictureBox thePictureBox;

Then fill in the reference in the form1

    Program.thePictureBox = pictureBox1;

Now you can reference it in all other forms as well:

myNextPictureBox42.Image = Program.thePictureBox.Image;


  • Both the pictureboxes are in different forms..I have alredy made them public. - VGI
  • Can you suggest any other method apart from this because I want to link many forms with one form in this case - VGI
  • Good! So all you need is at least one reference to the other form, like I explained. Then you can write, say in form1: pictureBox.Image = form2.pictureBox.Image. Of course your names are whatever you name them; something meanigfull I suggest, not just 1,2,3.. - TaW
  • I have already tried this..But this doesn't work - VGI
  • What did you try? I made several suggestions. Do append some relevant code to your question! And how does it not work? Compiler error? Runtime? You need to give more input here! - TaW

Linked


Latest