简体   繁体   English

我需要帮助在表单加载 c# 时填充 listBox 数据

[英]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.我正在处理一项任务,要求我创建一个名为 customer 的 class,该 class 将包括 3 个名为 FName、LName 和 Phone 的自动实现的属性、一个参数化的构造函数和一个 GetCustomer 方法。

I must then add a LIST of Customer objects to the project (not within the class) and call it CustomerDB.然后,我必须将 Customer 对象列表添加到项目中(不在类中)并将其命名为 CustomerDB。

Then create a LoadDB method, which adds the 4 new customer objects (data given in the table below) to the CustomerDB List.然后创建一个 LoadDB 方法,它将 4 个新客户对象(下表中给出的数据)添加到 CustomerDB 列表。 ie FIRST NAME LAST NAME PHONE Tom Doe 555-7654 Brad Pitt 555-6543 Jill Jack 555-5432 Pete Paul 555-1234即名字 姓氏 电话 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.我的困难是让 loadDB 方法在表单首次加载时用数据填充我的 lstBox。 I have been researching different online materials all day.我整天都在研究不同的在线资料。 Have finally resorted to asking here.终于求助于这里。 Thanks in advance I really appreciate the help.在此先感谢我非常感谢您的帮助。

You need to use a ToString overrided method in Customer class:您需要在Customer class 中使用ToString覆盖方法:

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.如果要保留CustomerDB变量,则需要将其移动到 class 级别。

public partial class Form1 : Form
{

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

  ...

}


在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM