C# 将form2列表元素访问到mainform

C# 将form2列表元素访问到mainform,c#,winforms,C#,Winforms,我有两张表格。。(Form1和form2)在Form1调用form2以执行某些操作的情况下,form2具有在操作或运行时添加了某些元素的列表,一旦form2的操作完成,我想将form2列表项复制到Form1中的列表 我使用ShowDialog()来显示form2,因为根据要求它是必需的 请告诉我如何定义列表,以便我可以从form1中访问添加到form2中的it元素 我没有任何要粘贴的代码。。。对不起 您可以在Form2中声明一个公共列表,并在完成/关闭Form2后将相关项目添加到此列表中 如果F

我有两张表格。。(Form1和form2)在Form1调用form2以执行某些操作的情况下,form2具有在操作或运行时添加了某些元素的列表,一旦form2的操作完成,我想将form2列表项复制到Form1中的列表

我使用ShowDialog()来显示form2,因为根据要求它是必需的

请告诉我如何定义列表,以便我可以从form1中访问添加到form2中的it元素


我没有任何要粘贴的代码。。。对不起

您可以在Form2中声明一个公共列表,并在完成/关闭Form2后将相关项目添加到此列表中

如果Form1引用了Form2对象,则可以从Form1访问该对象

所以在Form2中,你可以有

public partial class Form2 : Form
{
    public List<string> f2List = new List<string>();

    private void button1_Click(object sender, EventArgs e)
    {
        f2List.Add(f2List.Count.ToString());
    }
公共部分类表单2:表单
{
public List f2List=新列表();
私有无效按钮1\u单击(对象发送者,事件参数e)
{
添加(f2List.Count.ToString());
}
然后从Form1开始你可以试试

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.ShowDialog();
        List<string> f1List = f2.f2List;
    }
    public partial class Form1 : Form
    {
list<String> copy_of_form2_list;

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();

// for copy the object , we serialize and deserialize an object
                try
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    FileStream output = new FileStream("temp", FileMode.OpenOrCreate, FileAccess.Write);
                    formatter.Serialize(output,f2.f2List);
                    output.Close();
                }
                catch
                {

                }


                try
                {
                    BinaryFormatter reader = new BinaryFormatter();
                    FileStream input = new FileStream("temp", FileMode.Open, FileAccess.Read);
                    fcopy_of_form2_list=((List <String>)reader.Deserialize(input));
                    input.Close();

                    if (File.Exists(@"temp"))
                    {
                        File.Delete(@"temp");
                    }

                }
                catch
                {
                }

            List<string> f1List = copy_of_form2_list;
        }
公共部分类表单1:表单
{
私有无效按钮1\u单击(对象发送者,事件参数e)
{
Form2 f2=新的Form2();
f2.ShowDialog();
列表f1List=f2.f2List;
}
编辑

请参阅将项目添加到表单2中的操作,以便它们在表单1中可用。执行以下操作:

public partial class Form2 : Form
{
    public List<string> f2List = new List<string>();

    private void button1_Click(object sender, EventArgs e)
    {
        f2List.Add(f2List.Count.ToString());
    }
}
公共部分类表单2:表单
{
public List f2List=新列表();
私有无效按钮1\u单击(对象发送者,事件参数e)
{
添加(f2List.Count.ToString());
}
}
然后从Form1开始你可以试试

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.ShowDialog();
        List<string> f1List = f2.f2List;
    }
    public partial class Form1 : Form
    {
list<String> copy_of_form2_list;

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();

// for copy the object , we serialize and deserialize an object
                try
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    FileStream output = new FileStream("temp", FileMode.OpenOrCreate, FileAccess.Write);
                    formatter.Serialize(output,f2.f2List);
                    output.Close();
                }
                catch
                {

                }


                try
                {
                    BinaryFormatter reader = new BinaryFormatter();
                    FileStream input = new FileStream("temp", FileMode.Open, FileAccess.Read);
                    fcopy_of_form2_list=((List <String>)reader.Deserialize(input));
                    input.Close();

                    if (File.Exists(@"temp"))
                    {
                        File.Delete(@"temp");
                    }

                }
                catch
                {
                }

            List<string> f1List = copy_of_form2_list;
        }
公共部分类表单1:表单
{
列出表格2清单的副本;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
Form2 f2=新的Form2();
f2.ShowDialog();
//对于复制对象,我们序列化和反序列化一个对象
尝试
{
BinaryFormatter formatter=新的BinaryFormatter();
FileStream输出=新FileStream(“temp”,FileMode.OpenOrCreate,FileAccess.Write);
序列化(输出,f2.f2List);
output.Close();
}
抓住
{
}
尝试
{
BinaryFormatter reader=新的BinaryFormatter();
FileStream input=新FileStream(“temp”,FileMode.Open,FileAccess.Read);
fcopy_of_form2_list=((list)reader.Deserialize(input));
input.Close();
如果(File.Exists(@“temp”))
{
删除(@“temp”);
}
}
抓住
{
}
列表f1List=复制表单2列表;
}

存储在数据库中的列表项(即)您是否已在表格2中输入?否,项不是从数据库中添加的。我已在表格2中将列表定义为公共列表,但它返回零元素。您需要在表格2中的操作过程中的某个时间向该列表分配/添加值,以便以后可以在表格1中使用这些值