当我关闭它时将对象传递给主窗体 [英] pass object to main form when I close it

查看:46
本文介绍了当我关闭它时将对象传递给主窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Form2 中创建的对象(主窗体是 Form1).当我完成为 Form2 中的对象分配属性后,我希望它在 Form1 中可用.

I have an object that I create in Form2 (Main form is Form1). When I'm done assigning properties to the object in Form2, I would like to have it available in Form1.

对象是在Form2中创建的

The object is created in Form2

public partial class Form2 : Form
private List<POI> POIs_3D;

public Form2()
{
    InitializeComponent();
    POIs_3D = new List<POI>();
}

然后我为列表对象分配了一些值,并希望它在 Form1 中可用.

Then I assign add some values to the list object and want to have it available in Form1.

我知道这可能很简单,但我无法弄清楚..

I know it may be simple but I can't figure out..

这是打开 Form2 的代码:

This is the code that opens Form2:

private void btn_3d_Click(object sender, EventArgs e)
{
    Form formulario = new Form2();
    formulario.Show();
}

推荐答案

有很多方法可以做到这一点

There are many ways to do that

1)在Form1中声明对象.在创建时将其传递给 Form2.让 Form2 设置值.

1)Declare the object in Form1. Pass it to Form2 on creation. Have Form2 set the Value.

2) 在 Form2 内部创建一个属性/变量并让 Form1 读取该属性.如果您想要关闭 Form2 时的结果,这可能会有点复杂,因为您必须从 Form2 调用 Form1 中的方法才能获取值.

2)Make a property/variable internal in Form2 and have Form1 read the property. This can be a little complicated if you want the result on the closing of Form2 because you will have to call a method in Form1 from Form2 in order to get the value.

3) 在 Form1 内部创建一个属性/变量,并让 Form2 设置该值.这要求 Form2 知道 Form1 的实例.您可以在显示时在 Form2 的 Owner 属性中传递它.Form2.Show(this)

3)Make a Property/Variable internal in Form1 and have Form2 set that value. This requires that Form2 knows the instance of Form1. You can pass that in the Owner property of Form2 when showing. Form2.Show(this)

这篇关于当我关闭它时将对象传递给主窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆