Pedrum
Pedrum

Reputation: 644

How can I Change something in a Form1 from a DialogBox?

Lets say I have two forms ( Form1 And Form2 ).

Form1 is the main form, so when I run the project I see Form1.

How can I change the Image in the PictureBox in Form1 from Form2?

Upvotes: 3

Views: 497

Answers (5)

Alex Aza
Alex Aza

Reputation: 78487

Pass one form as a parameter to a constructor of the second form or add a method that passed the reference. After you have the reference to your form you can do whatever you want with the from.

Whether to share picture box as a public member is up to you. However, I would suggest making a public method SetImage() in the first form. Second form would call form1.SetImage().

[Update]

Wait, why do you need to set image from OpenFileDialog, you just need receive fileName from the dialog, and then open the file and load into the form.

Like this:

private void button1_Click(object sender, EventArgs e)
{
    using (var dialog = new OpenFileDialog())
    {
        var result = dialog.ShowDialog();
        if (result != DialogResult.OK)
            return;
        pictureBox1.Image = Image.FromFile(dialog.FileName);
    }
}

This is code inside of Form1.

[Update]

Here is the basic idea how to access one form from another.

public class MyForm1 : Form
{
    public MyForm1()
    {
        InitializeComponent();
    }

    public void SetImage(Image image)
    {
        pictureBox1.Image = image;
    }

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

public class MyForm2 : Form
{
    private MyForm1 form1;

    public MyForm2()
    {
        InitializeComponent();
    }

    public MyForm2(MyForm1 form1)
    {
        this.form1 = form1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        form1.SetImage(yourImage);
    }
}

Upvotes: 2

Alexander
Alexander

Reputation: 1297

You can very simply do this. At first change your code(in Form1) that show Form2 to looks like this:

<variable-of-type-Form2>.ShowDialog(this);

or if you dont want form2 to be modal

<variable-of-type-Form2>.Show(this);

Next when you got path to image, you can access pictureBox like this

((PictureBox)this.Owner.Controls["pictureBox1"]).Image=Image.FromFile(<filename>);

Assume that you have PictureBox on Form1 with name "pictureBox1"

Upvotes: 1

Peyman
Peyman

Reputation: 3138

In the Program.cs file you can set any value, either FormOptions to the form's instance .

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var frm = new Form1();
        // Add the code to set the picturebox image url
        Application.Run(frm);
    }

In addition you can add the Constructor to Form1 and pass the parameter via constructor.

Upvotes: 2

orangemoney52
orangemoney52

Reputation: 126

Ideally, you want to structure your code in a ModelViewController pattern. Then, you simply have a reference in your model to the image in the picture box. When interacting with the OpenFileDialog in Form2, you would call your model adapter interfaces hooked into the views (Form1 and Form2) to change the image being held in the model. In short, you need a callback from the view to the model. If you don't want to redesign your code to be MVC, simply have a shared object holding the image reference that both Form1 and Form2 receive in their constructors, so they can both modify it.

Upvotes: 1

Fourth
Fourth

Reputation: 9351

How do I pass a value from a child back to the parent form?

Basically, expose the value that gets returned in the open file dialog with some property and let the parent form grab it.

Upvotes: 2

Related Questions