我正在创建一个表格来保存来自“会议”的信息。用户将填写有关title、location、startTime、endTime、notes和date的信息。我目前正在处理的是“保存更改”按钮,它将:
title中显示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;
};发布于 2013-02-20 20:49:23
这就是我要重构全班的方式:
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结构格式存储,但这也是首选的问题。请注意,在将输入分配到会议对象之前,最好先对输入进行清理/验证(特别是日期)。
会议对象现在拥有属性而不是公共字段。如果您想要更改某物的获取/设置方式,则首选属性。
希望这能有所帮助。
发布于 2013-02-20 19:30:17
我确实建议您查找数据绑定,并学习如何正确地执行此操作,但是如果您想要一个快速而肮脏的解决方案(尽管最终您会发现这需要更多的工作),我会这样做:
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.
}
}https://stackoverflow.com/questions/14987390
复制相似问题