我有一个名为EventDB.cs的类,用于从文本文件中获取数据。我将每5行输入作为一个名为events的对象列表,但是在尝试将列表填充到我的主窗体上的组合框中时遇到了麻烦。有人能指出我做错了什么吗?这是我的Event.cs,EventDB.cs和main ticketinfo.cs的代码。提前谢谢!
Event.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;
}
}
}
EventDB.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;
}
}
}
Ticketinfo.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
答案 0 :(得分:0)
我认为你在Ticketinfo.cs中的CreateEventList()
方法中犯了错误。您根本不更新Ticketinfo.cs中的events
,您将始终为空列表。你应该换行
EventDB.ExtractData(events);
到
events = EventDB.ExtractData();
然后应该工作正常。现在你的方法将从文件中返回事件。
你的新方法应该是这样的:
private void CreateEventList()
{
events = EventDB.ExtractData(); //(mvwDate.SelectionStart);
cboEvent.Items.Clear();
foreach (Event e in events)
{
cboEvent.Items.Add(e.GetDisplayText());
}
} //end method CreateEventList
答案 1 :(得分:0)
如果您想要显示所选日期之前或之后的事件,您可以执行以下操作:
首先,您需要Events
课程中的实际日期 - 您有一天和一个时间,但我没有看到日期。因此,为了这个例子,我们只使用Day
属性。
您可以非常轻松地根据所选的Day
LINQ将事件列表绑定到组合框:
private void CreateEventList()
{
events = EventDB.ExtractData(); //(mvwDate.SelectionStart);
var e = (from ev in events
where ev.Day >= mvwDate.SelectionStart // mvwDate.SelectionStart needs to be an int
select ev).ToList();
cboEvent.Items.Clear();
cboEvent.DisplayMember = "StrDesc";
// You could also assign a ValueMember like this:
//cboEvent.ValueMember = "Day";
cboEvent.DataSource = e;
} //end method CreateEventList
在我的示例中有一些假设(主要是mvwDate.SelectionStart是什么),我还没有测试过代码,但这应该会给你另一种方法。