0

我有两个表格..(表格1和表格2)表格1正在调用表格2来执行一些操作,表格2的列表在操作或运行时添加了一些元素,一旦表格2的操作完成,我想要将form2列表项复制到form1中列出...

我正在使用ShowDialog()来显示 form2,因为根据要求它是强制性的。

请告诉我定义列表的方法,以便我可以从 form1 访问它添加到 form2 中的元素。

我没有要粘贴的代码...抱歉...

4

2 回答 2

0

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

这将可以从 Form1 访问,因为 Form1 具有对 Form2 对象的引用。

所以在 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());
    }

然后从 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;
    }

编辑

请参阅将项目添加到 Form2 中的列表的操作,以便它们在 Form1 中可用

于 2013-09-13T08:38:21.653 回答
0

做这个 :

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

然后从 Form1 你可以尝试

    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;
        }
于 2013-09-13T10:11:27.063 回答