【问题标题】:How to pass a list generated in one form to another form (C#)? [duplicate]如何将以一种形式生成的列表传递给另一种形式(C#)? [复制]
【发布时间】:2015-04-06 04:27:37
【问题描述】:

我有 2 种不同的表格,其中一种我生成客户列表,另一种我需要检索添加到列表中的信息。如何将列表传递给我的第二个表单?

这是第一个表格

    List<Customers> new_customer = new List<Customers>();

    private void newCustomer_Load(object sender, EventArgs e)
    {

    }

    private void fNameTxtBox_TextChanged(object sender, EventArgs e)
    {

    }

    private void lNameTxtBox_TextChanged(object sender, EventArgs e)
    {

    }

    private void addressTxtBox_TextChanged(object sender, EventArgs e)
    {

    }

    private void phoneNumTxtBox_TextChanged(object sender, EventArgs e)
    {

    }

    private void emailTxtBox_TextChanged(object sender, EventArgs e)
    {

    }

    private void IDTxtBox_TextChanged(object sender, EventArgs e)
    {

    }


    private void addNewCustButton_Click(object sender, EventArgs e)
    {

        if (fNameTxtBox.Text != "" && lNameTxtBox.Text != "" && addressTxtBox.Text != "" && phoneNumTxtBox.Text != "" && emailTxtBox.Text != "" && IDTxtBox.Text != "")
        {

            new_customer.Add(new Customers { FName = fNameTxtBox.Text, LName = lNameTxtBox.Text, Address = addressTxtBox.Text, phoneNum = phoneNumTxtBox.Text, emailAdd = emailTxtBox.Text, ID = int.Parse(IDTxtBox.Text) });
            MessageBox.Show("Thanks for Registering");
        }

        else
        {
            MessageBox.Show("Customer not added! Please fill out the entire form!");
        }

    }

}

}

这是第二种形式:

namespace WindowsFormsApplication1
{
public partial class Current_Customers : Form
{
    public Current_Customers()
    {
        InitializeComponent();

    }

    private void currCustComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
  }
}

【问题讨论】:

标签: c# .net winforms forms


【解决方案1】:

像这样创建 form2 的新构造函数,同时也创建第二个表单的列表。

public partial class Current_Customers : Form
{  
List<Customers> new_customer = new List<Customers>();
public Current_Customers(List<Customers> customers)
{
 new_customer=customers;
}
}

当您将在 form1 中创建此表单的对象时,请执行此操作

 Current_Customers cus=new Current_Customers(new_customer);

这会将列表传递给第二种形式。

【讨论】:

  • 您好,Mairaj,感谢您的回复。我尝试实现这一点,但是当我需要在单击父表单上的按钮时创建此表单的实例时,我需要将一个列表传递给它的构造函数(因为我们在 form2 上引入了构造函数方法)。我尝试了该列表,它说该列表在该上下文中不可用。我该如何解决这个问题?
【解决方案2】:

您有两种可能的方式来做到这一点。

1) 在两个表单上将列表设为公共字段/属性。如果两种形式存在于同一范围内,则它们可以相互引用。

2) 将列表添加到第三个类,最好是静态类,这两种形式都可以访问。这是我个人的喜好。

public static class StaticData
{
    public static readonly List<Customers> _Customers = new List<Customers>();

    public static List<Customers> CustomerList
    {
         get 
         {
            if (_Customers.Count < 1) 
            {
                  //Load Customer data
            }
            return _Customers;
         }
    }
}

public class Form1
{
    private List<Customers> new_customer = null;
    public Form1()
    {
        this.new_customer = StaticData.CustomerList;
    }
}

public class Current_Customers
{
    private List<Customers> new_customer = null;

    public Current_Customers()
    {
        this.new_customer = StaticData.CustomerList;
    }
}

尽管我的例子并不是真正的线程安全,但它只是为了给你指明正确的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多