如何从对话框更改 Form1 中的某些内容?

发布于 2024-11-09 11:49:57 字数 193 浏览 0 评论 0原文

假设我有两个表单( Form1 和 Form2 )。

  • Form1 有一个 PictureBox
  • Form2 我有一个 OpenFileDialog

Form1 是主窗体,所以当我运行该项目时我看到 Form1。

如何更改Form1中PictureBox中的图像从Form2?

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

  • Form1 has a PictureBox
  • Form2 I has an OpenFileDialog

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

波浪屿的海角声 2024-11-16 11:49:57

如何将值从子窗体传递回父窗体?

基本上,使用某些属性公开在打开的文件对话框中返回的值,并让父窗体获取它。

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.

单挑你×的.吻 2024-11-16 11:49:57

在 Program.cs 文件中,您可以设置任何值,可以将 FormOptions 设置为表单的实例。

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

此外,您可以将构造函数添加到 Form1 并通过构造函数传递参数。

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.

〗斷ホ乔殘χμё〖 2024-11-16 11:49:57

将一个表单作为参数传递给第二个表单的构造函数,或者添加一个传递引用的方法。获得对表单的引用后,您可以对表单进行任何您想要的操作。

是否以公共成员身份共享图片框由您决定。但是,我建议在第一种形式中创建一个公共方法 SetImage() 。第二个表单将调用 form1.SetImage()

[更新]

等等,为什么你需要从OpenFileDialog设置图像,你只需要从对话框中接收fileName,然后打开文件并加载到表单中。

像这样:

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);
    }
}

这是 Form1 内部的代码。

[更新]

以下是如何从一种表单访问另一种表单的基本思想。

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);
    }
}

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);
    }
}
弱骨蛰伏 2024-11-16 11:49:57

你可以非常简单地做到这一点。
首先将显示 Form2 的代码(在 Form1 中)更改为如下所示:

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

或者如果您不希望 form2 为模态

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

接下来当您获得图像路径时,您可以像这样访问图片框

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

假设您在 Form1 上有名为“的 PictureBox”图片框1"

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"

纵性 2024-11-16 11:49:57

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文