2013-02-20 54 views
0

我正在创建一个表单以保存来自“会议”的信息。用户将填写有关titlelocation,startTime,endTime,notesdate的信息。我目前的工作是“保存更改”按钮,它将:C#会议系统

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

我已经完成#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; 
}; 
+4

这不是一个人们为你编写代码的地方,你需要展示*你尝试过的*,如果你有任何具体的问题/问题,那么你将得到积极的回应。 – AbZy 2013-02-20 18:36:13

+0

这里是我试过的:meetingListBox.Text = meetingArray [currentIndex]。标题; – PrgmRNoob 2013-02-20 18:39:02

+2

虽然家庭作业标签现已过时,但您应该提及这是否是家庭作业。 – Pete 2013-02-20 18:40:03

回答

0

这是我会怎样重构类:

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结构格式中,但这又是一个偏好问题。请注意,在将其分配到会议对象之前,应对其进行清理/验证输入(尤其是日期)。

会议对象现在具有属性而不是公共字段。如果您想要更改Get/Set的内容,最好使用属性。

希望这会有所帮助。

0

我真的建议你看看数据绑定,并学习如何正确地做到这一点,但如果你想快速和肮脏的解决方案(虽然,到最后,你会发现这是一个很多工作),我会做这样的事情:

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. 
    } 
} 
+0

马特...感谢一堆!你的洞察力和知识已经非常有帮助:) – PrgmRNoob 2013-02-20 19:49:37