我有一个名为 EventDB.cs 的类,它从文本文件中获取数据。我将每 5 行输入为一个名为 events 的对象列表,但在尝试将列表填充到我的主窗体上的组合框中时遇到了麻烦。有人可以指出我做错了什么吗?这是我的 Event.cs、EventDB.cs 和 main ticketinfo.cs 的代码。提前致谢!
事件.cs
namespace TicketInformation
{
public class Event
{
public Event()
{
}
public Event(int day, string time, double price, string strEvent, string description)
{
this.Day = day;
this.Time = time;
this.Price = price;
this.StrEvent = strEvent;
this.Description = description;
}
public int Day { get; set; }
public string Time { get; set; }
public double Price { get; set; }
public string StrEvent { get; set; }
public string Description { get; set; }
public string GetDisplayText()
{
return StrEvent;
}
}
}
事件数据库.cs
namespace TicketInformation
{
public static class EventDB
{
private static string dir = Directory.GetCurrentDirectory();
private static string path = dir + "\\calendar.txt";
public static List<Event> ExtractData() //(DateTime dtmDay)
{
//int intChosenDay = dtmDay.Day;
// create object for input stream for text file
StreamReader textIn =
new StreamReader(
new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
//create the list
List<Event> events = new List<Event>();
string[] lines = File.ReadAllLines(path);
for (int index = 4; index < lines.Length; index += 5)
{
Event special = new Event();
special.Day = Convert.ToInt32(lines[index - 4]);
special.Time = (lines[index - 3]);
special.Price = Convert.ToDouble(lines[index - 2]);
special.StrEvent = lines[index - 1];
special.Description = lines[index];
events.Add(special);
}
//close stream for the text file
textIn.Close();
return events;
}
}
}
票务信息.cs
static void Main()
{
Application.Run(new FrmEvents());
}
private List<Event> events = null;
private void FrmEvents_Load(
object sender, System.EventArgs e)
{
CreateEventList();
}
private void CreateEventList()
{
EventDB.ExtractData(events); //(mvwDate.SelectionStart);
cboEvent.Items.Clear();
foreach (Event e in events)
{
cboEvent.Items.Add(e.GetDisplayText());
}
} //end method CreateEventList