C# 加载方法流
我有一个应用程序,允许您编辑员工详细信息,添加新的受薪员工和新的小时制员工。我试图从文本文件中获取数据,以显示在windows窗体应用程序的列表框中。这是我主窗体上的代码C# 加载方法流,c#,winforms,load,C#,Winforms,Load,我有一个应用程序,允许您编辑员工详细信息,添加新的受薪员工和新的小时制员工。我试图从文本文件中获取数据,以显示在windows窗体应用程序的列表框中。这是我主窗体上的代码 namespace HRApplication { public partial class MainForm : Form { // The file used to store employee details string employeesFile = "employees.txt"; //
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;
公共bool加载(字符串employeesFile)
{
列表=新列表();
使用(StreamReader=newstreamreader(“employees.txt”))
{
弦线;
而((line=reader.ReadLine())!=null)
{
列表。添加(行);
控制台写入线(行);
}
}
返回true;
文本文件中的数据不会显示,我如何让它显示?谢谢您的
控制台.WriteLine
打印了什么吗?我的文本文件中有数据?用Debug.WriteLine更改Console.WriteLine。然后查看输出窗口中是否确实从employees.txt文件中读取了任何数据。然后告诉我们如何传递本地列表变量e、 您在Load方法中使用字符串填充PopulateListBox方法,因为您编写的内容并不清楚。