C#会议系统

本文关键字:会议系统 | 更新日期: 2023-09-27 18:00:22

我正在创建一个表单来保存"会议"中的信息。用户将填写关于titlelocationstartTimeendTimenotesdate的信息。我目前正在做的是"保存更改"按钮,它将:

  1. 清除所有文本框
  2. 将输入存储在数组中
  3. 在ListBox中仅显示title
  4. 当在ListBox中单击标题时,如果用户希望进行更改,则存储在该数组元素中的信息将重新填充到相应的TextBoxes中

我已经完成了#1、#2和#3。如果能为#4提供任何帮助,我将不胜感激。我已经粘贴了下面的编码供您查看。

public partial class CalendarForm : Form
{
    int currentIndex;
    int arraySize = 0;
    Meeting[] meetingArray = new Meeting[100];
    public CalendarForm()
    {
        InitializeComponent();
    }
    private void saveChangesButton_Click(object sender, EventArgs e)
    {
        meetingArray[arraySize] = new Meeting();
        meetingArray[arraySize].title = textBoxTitle.Text;
        meetingArray[arraySize].location = textBoxLocation.Text;
        meetingArray[arraySize].startTime = textBoxStartTime.Text;
        meetingArray[arraySize].endTime = textBoxEndTime.Text;
        meetingArray[arraySize].notes = notesTextBox.Text;
        currentIndex = arraySize;
        arraySize++;
        meetingListBox.Enabled = true;
        textBoxTitle.Text = "";
        textBoxLocation.Text = "";
        textBoxStartTime.Text = "";
        textBoxEndTime.Text = "";
        notesTextBox.Text = "";
       *edit* added these two lines which now add the title to the listBox
       meetingListBox.Items.Add(meetingArray[currentIndex].title);
        Controls.Add(meetingListBox);
    }
}
public class Meeting
{
    public string title;
    public string location;
    public string startTime;
    public string endTime;
    public string notes;
};

C#会议系统

这就是我重构类的方式:

public partial class CalendarForm : Form
{
  private List<Meeting> Meetings { get; set; }
  public CalendarForm()
  {
    InitializeComponent();
    Meetings = new List<Meeting>();
  }
  private void saveChangesButton_Click(object sender, EventArgs e)
  {     
    try 
    {       
      Meeting meeting = CreateMeeting();
      Meetings.Add(meeting);
      meetingListBox.Add(meeting);
    }
    catch
    {
      //Add proper error handling here
    }            
  }
  private Meeting CreateMeeting()
  {
    return new Meeting()
    {
      Title = textBoxTitle.Text,
      Location = textBoxLocation.Text
      StartTime = DateTime.Parse(textBoxStartTime.Text),
      EndTime = DateTime.Parse(textBoxEndTime.Text),
      Notes = notesTextBox.Text,
     };
   }
}
//As Matt Burland answered already:
private void meetingListBox_SelectedIndexChanged(object sender, EventArgs e)
{
  Meeting meeting = meetingListBox.SelectedItem as Meeting;
  if (meeting != null) 
  {
    textBoxTitle.Text = meeting.Title;
    //...etc for all your other text boxes.
  }
}
public class Meeting
{
  public string Title { get; set; }
  public string Location  { get; set; }
  public DateTime StartTime  { get; set; }
  public DateTime EndTime  { get; set; }
  public string Notes  { get; set; }
  public override string ToString()
  {
    return Title; 
  }
}

我做了很多更改,尤其是从数组切换到列表<>。列表更加灵活,提供了更好的功能。除非您真的需要使用数组,否则我会远离它们,只是为了更好地防止逻辑错误索引越界类型问题。

此外,我个人认为日期应该以DateTime结构格式存储,但这也是一个偏好问题。请注意,在将输入(尤其是日期)分配到Meeting对象之前,谨慎的做法是对其进行消毒/验证。

Meeting对象现在具有属性,而不是公共字段。如果您想要更改某些内容的获取/设置方式,则首选属性。

希望这能有所帮助。

我真的建议你查找数据绑定并学习如何正确地完成这项工作,但如果你想要一个快速而肮脏的解决方案(尽管最终你会发现它需要做更多的工作),我会做这样的事情:

private void saveChangesButton_Click(object sender, EventArgs e)
{
    Meeting m = new Meeting();
    m.title = textBoxTitle.Text;
    m.location = textBoxLocation.Text;
    m.startTime = textBoxStartTime.Text;
    m.endTime = textBoxEndTime.Text;
    m.notes = notesTextBox.Text;
    meetingArray[arraySize] = m;
    currentIndex = arraySize;
    arraySize++;
    meetingListBox.Enabled = true;
    textBoxTitle.Text = "";
    textBoxLocation.Text = "";
    textBoxStartTime.Text = "";
    textBoxEndTime.Text = "";
    notesTextBox.Text = "";
    meetingListBox.Items.Add(m);
    //Controls.Add(meetingListBox);   // You don't need to keep adding the control every time!

}

现在,在您的Meeting类中,我将覆盖ToString(),只返回标题。默认情况下,ListBox将只使用添加到其中的ToString()方法。

为了帮助#4,您希望绑定SelectedIndexChanged事件,然后使用SelectedItem属性,将其强制转换回Meeting对象(因为它将返回Object),然后使用它来重新填充各种文本框。

类似于:

private void meetingListBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
    Meeting m = meetingListBox.SelectedItem as Meeting;
    if (m != null) 
    {
        textBoxTitle.Text = m.title;
        //...etc for all your other text boxes.
    }
}