文件加载和添加到列表
本文关键字:列表 添加 加载 文件 | 更新日期: 2023-09-27 18:13:17
我基本上试图将项目从。txt文件添加到listbox
。问题是加载.txt文件的方法是在一个单独的类中,所以我到了一个点,我不知道该怎么做。任何帮助都将非常感激。下面是这个类,带有文件加载方法:
public class BunchOfDeliverables
{
private List<Person> myPersons;
private List<Deliverable> myDeliverables;
public BunchOfDeliverables()
{
this.myPersons = new List<Person>();
this.myDeliverables = new List<Deliverable>();
}
public List<Person> Persons { get { return this.myPersons; } }
public List<Deliverable> Deliverables { get { return this.myDeliverables; } }
public void LoadPersonsFromFile(String filename)
{
StreamReader sr = null;
try
{
sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
String name, street, housenr, postalcode, city;
name = sr.ReadLine();
while (name != null)
{
street = sr.ReadLine();
housenr = sr.ReadLine();
postalcode = sr.ReadLine();
city = sr.ReadLine();
this.myPersons.Add(new Person(name, street, Convert.ToInt32(housenr), postalcode, city));
name = sr.ReadLine();
name = sr.ReadLine(); //and again read a line, because of the delimiter (line with the stars)
}
}
catch (IOException) { }
finally
{
if (sr != null) sr.Close();
}
}
public void LoadDeliverablesFromFile(String filename)
{
StreamReader sr = null;
try
{
sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
String s;
s = sr.ReadLine();
while (s != null)
{
String[] items = s.Split();
this.myDeliverables.Add(new Deliverable(Convert.ToInt32(items[0]), Convert.ToInt32(items[1]), this.myPersons[Convert.ToInt32(items[2])]));
s = sr.ReadLine();
}
}
catch (IOException) { }
finally
{
if (sr != null) sr.Close();
}
}
public void AddPerson(Person p)
{
this.myPersons.Add(p);
}
public Deliverable FindDeliverable(int id)
{
foreach (Deliverable d in this.myDeliverables)
{
if (d.ID == id)
{
return d;
}
}
return null;
}
public void AddDeliverable(Deliverable d)
{
if (FindDeliverable(d.ID) == null)
{
myDeliverables.Add(d);
}
else
{
throw new Exception("Be aware: nothing is added!!!");
}
}
然后这里是表单类(大部分为空):
public partial class Form1 : Form
{
BunchOfDeliverables d;
public Form1()
{
InitializeComponent();
d = new BunchOfDeliverables();
}
private void AddLoadedFilesToListbox_Click(object sender, EventArgs e)
{
}
Edit :
我尝试了以下操作,但仍然不起作用:
public partial class Form1: Form{BunchOfDeliverables d;公共Form1 (){InitializeComponent ();d = new bunchof可交付成果();d.LoadDeliverablesFromFile(". .数据/deliverables.txt");}
private void button1_Click(object sender, EventArgs e)
{
foreach (Deliverable deliv in d.Deliverables)
{
listBox1.Items.Add(deliv);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
就是这么简单:
BunchOfDeliverables bunchOfDeliverables = new BunchOfDeliverables();
bunchOfDeliverables.LoadPersonsFromFile(personsFile);
bunchOfDeliverables.LoadDeliverablesFromFile(deliverablesFile);
listBox.DataSource = bunchOfDeliverables.Persons;
listBox.DisplayMember = "<Whatever>";
listBox.ValueMember = "<Whatever>";
// OR
listBox.DataSource = bunchOfDeliverables.Deliverables;
listBox.DisplayMember = "<Whatever>";
listBox.ValueMember = "<Whatever>";
我不知道你使用的是哪个列表(Persons
还是Deliverables
),所以代码中包含了这两个列表。