0

I have a 2 different forms, in one I generate a list of customers, in the other one I need to retrieve the information added into the list. How can I pass the list to my second form?

Here's the first form

    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!");
        }

    }

}

}

And here's the second form:

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

    }

    private void currCustComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
  }
}
Hamed Syh
  • 1
  • 1
  • 1
  • 3
  • Using this you can pass value from one form to another form http://www.codeproject.com/Questions/180489/How-to-copy-all-the-items-between-listboxes-in-two – Pradnya Bolli Apr 06 '15 at 04:44

2 Answers2

1

Create a new constructor of form2 like this and also create a list in second form as well.

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

And when you will create object of this form in form1 do this

 Current_Customers cus=new Current_Customers(new_customer);

This will pass list to second form.

Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
  • Hi Mairaj, thanks for your response. I tried implementing this, however when I need to create an instance of this form when a button on the parent form is clicked, I will need to pass a list to it's constructor (because of the constructor method we introduced on form2). I tried the list, and it says the list is not available in that context. How can I go about to fix this? – Hamed Syh Apr 06 '15 at 18:00
0

You have two possible ways of doing this.

1) Make the lists public fields / Properties on both forms. If both forms exist in the same scope, they can reference each other.

2) Add the list to a third class, preferably a static class, that both forms can access. This would be my personal preference.

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

It has to be though that my example here is not really threadsafe and is meant to just point you in the right direction.

Peter Lange
  • 2,786
  • 2
  • 26
  • 40