TechNoob
TechNoob

Reputation: 1

I need help Populating listBox data on form load c#

Im working on a task that asks me to create a class named customer, that class will include 3 auto implemented properties named FName, LName and Phone, a parameterized constructor and a GetCustomer method.

I must then add a LIST of Customer objects to the project (not within the class) and call it CustomerDB.

Then create a LoadDB method, which adds the 4 new customer objects (data given in the table below) to the CustomerDB List. ie FIRST NAME LAST NAME PHONE Tom Doe 555-7654 Brad Pitt 555-6543 Jill Jack 555-5432 Pete Paul 555-1234

This method should be called when the form loads, thus populating the list with the data.

This is what i have so far.

`public partial class Form1 : Form
 {


    private string fName, lName, phone;

    public Form1()
    {
        InitializeComponent();

    }




    private void Form1_Load(object sender, EventArgs e)
    {
        LoadDB();
    }


    private void LoadDB()
    {


        List<Customer> CustomerDB = new List<Customer>();


        CustomerDB.Add(new Customer { FName = "Tom", LName = "Doe", Phone = "555-7654" });
        CustomerDB.Add(new Customer { FName = "Brad", LName = "Pitt", Phone = "555-6543" });
        CustomerDB.Add(new Customer { FName = "Jill", LName = "Jack", Phone = "555-5432" });
        CustomerDB.Add(new Customer { FName = "Peter", LName = "Paul", Phone = "555-4321" });




              }
         }

    class Customer
    {
    public string FName { get; set; }
    public string LName { get; set; }
    public string Phone { get; set; }

    public Customer(string fN, string lN, string ph)

    {
        FName = fN;
        LName = lN;
        Phone = ph;
    }




    public string GetCustomer()
    {

        return "FirstName: " + FName + "   LastName: " + LName + "  Phone: " + Phone;
    }
}

}

My struggle is getting the loadDB method to populate my lstBox with data when the form first loads. I have been researching different online materials all day. Have finally resorted to asking here. Thanks in advance I really appreciate the help.

Upvotes: 0

Views: 383

Answers (1)

user12031933
user12031933

Reputation:

You need to use a ToString overrided method in Customer class:

public class Customer
{

    public override string ToString()
    {
        return GetCustomer();
    }

    ...

}

So the list can show each items as you need.

You can adapt for what you want the list must display.

To use the constructor and to populate the listbox you need to write:

private void LoadDB()
{
  List<Customer> CustomerDB = new List<Customer>();

  CustomerDB.Add(new Customer("Tom", "Doe", "555-7654"));
  CustomerDB.Add(new Customer("Brad", "Pitt", "555-6543"));
  CustomerDB.Add(new Customer("Jill", "Jack", "555-5432"));
  CustomerDB.Add(new Customer("Peter", "Paul", "555-4321"));

  listBox1.Items.AddRange(CustomerDB.ToArray());
}

If you want to keep the CustomerDB variable, you need to move it at the class level.

public partial class Form1 : Form
{

  List<Customer> CustomerDB = new List<Customer>();

  ...

}


enter image description here

Upvotes: 1

Related Questions