将文本文件加载到列表框

时间:2017-05-28 20:56:22

标签: c# winforms stream load

我正在尝试将数据加载到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的类,我试图让load方法在这里运行是我的代码,任何帮助都会非常有帮助。

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

1 个答案:

答案 0 :(得分:0)

为简单起见,假设您的Employee类是这样的。

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

现在,当您从文件加载一行文本时,您应该将其拆分为代表员工数据的部分。使用该数据创建employee类的实例,并将实例添加到Employees的基类(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()重载来隐藏ListBox文本的构建)

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