0

テキスト ファイルからデータを取得する EventDB.cs というクラスがあります。イベントと呼ばれるオブジェクト リストとして 5 行ごとに入力していますが、メイン フォームのコンボボックスにリストを入力しようとして問題が発生しています。誰かが私が間違っていることを指摘できますか? これが、私の Event.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
4

2 に答える 2

0

選択した日付以降のイベントを表示する場合は、いくつかの方法があります。

まず、Eventsクラスの実際の日付が必要です。日付と時刻がありますが、日付が表示されません。Dayしたがって、この例の目的のためにプロパティを使用しましょう。

DayLINQで選択したものに基づいて、イベント リストをコンボ ボックスに非常に簡単にバインドできます。

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 に答える