我有一个应用程序,可让您编辑员工详细信息,添加新的受薪员工和新的每小时员工。我试图从文本文件中获取数据,以显示在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;
}
我来自另一个班级的加载方法:
public bool Load(string employeesFile)
{
List<String> list = new List<String>();
using (StreamReader reader = new StreamReader("employees.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line);
Console.WriteLine(line);
}
}
return true;
文本文件中的数据不会显示,如何显示?谢谢