将文本文件加载到列表框中

Loading a text file into a listbox

我正在尝试将数据加载到 c# 中 windows 表单应用程序的列表框中。我有来自主窗体的代码,它将数据放入列表框,如下所示...

namespace HRApplication
{
public partial class MainForm : Form
{
    // The file used to store employee details
    string employeesFile = "employees.txt";

    // The collection used to hold the employee data
    Employees employees;

    public MainForm()
    {
        InitializeComponent();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        employees = new Employees();
        if (!employees.Load(employeesFile))
        {
            MessageBox.Show("Unable to load employees file");
        }
        else
        {
            PopulateListBox();
        }
    }

    private void PopulateListBox()
    {
        listBoxEmployees.Items.Clear();

        foreach (Employee employee in employees)
        {
            listBoxEmployees.Items.Add(employee.lastName + ", " + 
         employee.firstName);
        }
        //listBoxEmployees.SelectedIndex = 0;
    }

由此,我有一个名为 Employees 的 class,我试图让加载方法在这里工作,这是我拥有的代码,任何帮助都会非常有帮助。

namespace HRApplication
{
public class Employees : List<Employee>
{ 

    public Employees()
    { }


    public bool Load(string employeesFile)
    {
        List<string> lines = new List<string>();

        using (StreamReader reader = new StreamReader("employees.txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                lines.Add(line);
            }
            return true;
           }
        }
    }
}

为了简单起见,假设您的员工 class 是这样的。

public class Employee
{
    public string lastName {get;set;}
    public string firstName {get;set;}
    public override string ToString()
    {
        return lastName + ", " + firstName;
    }
}

现在,当您从文件中加载一行文本时,您应该将其拆分为代表员工数据的部分。使用该数据创建员工 class 的实例并将该实例添加到员工的基础 class (a List<Employee>)

public bool Load(string employeesFile)
{
    using (StreamReader reader = new StreamReader("employees.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            // Suppose that your line is splittable using a comma...
            string[] temp = line.Split(',');

            // This is really simplicistic. In a real world scenario
            // you should check if the line contains correctly all the 
            // fields required to populate an Employee...
            Employee emp = new Employee() 
            { 
                 firstName = temp[0], 
                 lastName=temp[1]
            };

            // This class is derived from List<T>
            // so you can use the Add method 
            // to add the employee to itself...
            Add(emp);
        }
        return true;
       }
    }
}

此时填充列表框的循环应该可以工作(我使用 ToString() 重载来隐藏列表框文本的构建)

private void PopulateListBox()
{
    listBoxEmployees.Items.Clear();
    foreach (Employee employee in employees)
    {
        listBoxEmployees.Items.Add(employee);
    }
    //listBoxEmployees.SelectedIndex = 0;
}