首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#会议系统

C#会议系统
EN

Stack Overflow用户
提问于 2013-02-20 18:33:45
回答 2查看 172关注 0票数 0

我正在创建一个表格来保存来自“会议”的信息。用户将填写有关titlelocationstartTimeendTimenotesdate的信息。我目前正在处理的是“保存更改”按钮,它将:

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

我已经完成了#1,#2和#3,我希望能为#4提供任何帮助,我已经粘贴了下面的代码供您查看。

代码语言:javascript
复制
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;
};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-20 20:49:23

这就是我要重构全班的方式:

代码语言:javascript
复制
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; 
  }
}

我做了一些更改,尤其是从数组到List<>的切换。列表更灵活,并提供更好的功能。除非您真的需要使用数组,否则我将远离它们,以便更好地防止逻辑错误索引超出界限类型的问题。

此外,我个人认为日期应该以DateTime结构格式存储,但这也是首选的问题。请注意,在将输入分配到会议对象之前,最好先对输入进行清理/验证(特别是日期)。

会议对象现在拥有属性而不是公共字段。如果您想要更改某物的获取/设置方式,则首选属性。

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2013-02-20 19:30:17

我确实建议您查找数据绑定,并学习如何正确地执行此操作,但是如果您想要一个快速而肮脏的解决方案(尽管最终您会发现这需要更多的工作),我会这样做:

代码语言:javascript
复制
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),然后使用它重新填充各种文本框。

类似于:

代码语言:javascript
复制
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.
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14987390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档