我正在创建一个表单来保存“会议”中的信息。用户将填写有关title
,location
,startTime
,endTime
,notes
和date
的信息。我目前正在处理的是“保存更改”按钮,它将:
title
。 我已经完成了#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;
};
答案 0 :(得分: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
对象(因为它将返回{ {1}})然后用它来重新填充各种文本框。
类似的东西:
Object
答案 1 :(得分: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;
}
}
我做了很多更改,更值得注意的是从数组切换到List&lt;&gt;。列表更灵活,提供更好的功能。除非你真的需要使用数组,否则我会远离它们,以便更好地防止逻辑错误索引超出范围类型问题。
另外,我个人认为日期应该以DateTime结构格式存储,但这又是一个偏好问题。请注意,在将输入分配给Meeting对象之前,清理/验证输入(尤其是日期)是明智的。
Meeting对象现在具有属性而不是公共字段。如果您想要更改某些内容的获取/设置,则首选属性。
希望这会有所帮助。