[英]how do I get my method in eventdb.cs to populate my combobox in my main form?
我有一個名為EventDB.cs的類,該類可從文本文件中獲取數據。 我每5行輸入作為一個稱為事件的對象列表,但是在嘗試將列表填充到主窗體上的組合框中時遇到了麻煩。 有人可以指出我做錯了嗎? 這是我的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
我認為您在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是什么),並且我尚未測試代碼,但這應該為您提供另一種方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.