简体   繁体   English

如何将2个对象从Form1传递到Form2

[英]how to pass 2 objects from form1 to form2

how to pass 2 object from form1 to form2 in winforms using C# 如何使用C#在Winforms中将2个对象从Form1传递到Form2

i know to pass one object like this: 我知道要传递这样的一个对象:

Form G = new frm2(sc);
G.ShowDialog();

thank in advance 预先感谢

Make constructor with 2 arguments like below : 使构造函数具有如下两个参数:

public partial class frm2 :Form
    {
         public frm2(List<int> object1, List<string> object2)
        {
        }

         .....
         ......
    }

And call like : 像这样打电话:

Form G = new frm2(object1, object2); 
G.ShowDialog(); 

Second Option : 第二种选择:

You can make properties in forms and pass it like below : 您可以在表单中创建属性,然后将其传递如下:

Form G = new frm2(); 
G.Object1 = object1;
G.Object2 = object2;
G.ShowDialog(); 

Your frm2 will be like this : 您的frm2将如下所示:

public partial class frm2 : Form
{
     public frm2()
    {
    }

     public List<int> Object1 { get; set; }
     public List<string> Object2 { get; set; }
     .....
     ......
}

Why don't you make a constructor taking two arguments? 为什么不让构造函数接受两个参数?

Form G = new frm2(obj1, obj2);
G.ShowDialog();

With frm2: 使用frm2:

public partial class frm2 : Form {

    private Object1 _obj1;
    private Object2 _obj2;

    void frm2(Object1 obj1, Object2 obj2) {
        this._obj1 = obj1;
        this._obj2 = obj2;
    }
}

just modify the constructor of frm2 to accept 2 parameters: 只需修改frm2的构造函数以接受2个参数即可:

public void frm2(object obj1, object obj2)
{
    //to do...
}

The second method is to write an own constructor which accepts those two objects. 第二种方法是编写一个自己的构造函数,该构造函数接受这两个对象。 If it's mandatory for the form, then hide the default constructor and with that force the passing of the two objects. 如果对于表单是必选的,则隐藏默认构造函数,并强制传递两个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM