如何从对话框更改 Form1 中的某些内容?
假设我有两个表单( 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如何将值从子窗体传递回父窗体?
基本上,使用某些属性公开在打开的文件对话框中返回的值,并让父窗体获取它。
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.
在 Program.cs 文件中,您可以设置任何值,可以将 FormOptions 设置为表单的实例。
此外,您可以将构造函数添加到 Form1 并通过构造函数传递参数。
In the Program.cs file you can set any value, either FormOptions to the form's instance .
In addition you can add the Constructor to Form1 and pass the parameter via constructor.
将一个表单作为参数传递给第二个表单的构造函数,或者添加一个传递引用的方法。获得对表单的引用后,您可以对表单进行任何您想要的操作。
是否以公共成员身份共享图片框由您决定。但是,我建议在第一种形式中创建一个公共方法
SetImage()
。第二个表单将调用form1.SetImage()
。[更新]
等等,为什么你需要从OpenFileDialog设置图像,你只需要从对话框中接收fileName,然后打开文件并加载到表单中。
像这样:
这是 Form1 内部的代码。
[更新]
以下是如何从一种表单访问另一种表单的基本思想。
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 callform1.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:
This is code inside of Form1.
[Update]
Here is the basic idea how to access one form from another.
你可以非常简单地做到这一点。
首先将显示 Form2 的代码(在 Form1 中)更改为如下所示:
或者如果您不希望 form2 为模态
接下来当您获得图像路径时,您可以像这样访问图片框
假设您在 Form1 上有名为“的 PictureBox”图片框1"
You can very simply do this.
At first change your code(in Form1) that show Form2 to looks like this:
or if you dont want form2 to be modal
Next when you got path to image, you can access pictureBox like this
Assume that you have PictureBox on Form1 with name "pictureBox1"
理想情况下,您希望在 ModelViewController 模式。然后,您只需在模型中引用图片框中的图像即可。当与 Form2 中的 OpenFileDialog 交互时,您将调用连接到视图(Form1 和 Form2)的模型适配器接口来更改模型中保存的图像。简而言之,您需要从视图到模型的回调。如果您不想将代码重新设计为 MVC,只需使用一个共享对象来保存 Form1 和 Form2 在其构造函数中接收的图像引用,以便它们都可以修改它。
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.