我如何得到我的方法在eventdb.cs填充我的组合框在我的主要形式
本文关键字:我的 组合 填充 eventdb 何得 方法 cs | 更新日期: 2023-09-27 18:11:00
我有一个名为EventDB.cs的类,它从文本文件中获取数据。我有每5行输入作为一个对象列表称为事件,但我有麻烦试图填充列表到我的组合框上我的主要形式。有人能指出我做错了什么吗?这是我的代码为我的事件。cs, EventDB.cs和主要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
我认为您在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
如果您想显示在选定日期当天或之后发生的事件,您可以做以下几件事:
首先,你需要一个实际的日期在你的Events
类-你有一个日期和时间,但我没有看到一个日期。因此,在本例中我们只使用Day
属性。
你可以很容易地用LINQ将你的事件列表绑定到你的组合框中,基于所选择的Day
:
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是),我还没有测试代码,但这应该给你另一种方法。