0

我有一个名为 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
4

2 回答 2

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 是什么),我还没有测试过代码,但这应该会给你另一种方法。

于 2013-04-25T07:50:34.333 回答
0

我认为您CreateEventList()在 Ticketinfo.cs 中的方法有误。您根本不会在 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
于 2013-04-25T07:20:45.973 回答