我创建了一个表单,它接受用户输入的几个不同信息(标题、位置、日期(来自一个月日历)等),当单击添加按钮时,信息存储在当前数组元素和标题中显示在列表框中。当列表框中的标题被选中时,该特定标题的其余信息将重新填充到相应的文本框中。
我一直试图更进一步,但没有成功。单击“添加”按钮时,我希望将用户输入保存到月份日历上选择的日期。因此,如果用户单击没有保存信息的日期,则 listBox 保持为空。如果在某个日期保存了信息,则列表框将显示标题。
代码片段:
class MeetingManager
{
private Meeting[] meetings;
public int currentIndex;
public int maxIndex;
private string title;
private string location;
private string startTime;
private string endTime;
private string notes;
public MeetingManager()
{
meetings = new Meeting[10];
currentIndex = -1;
maxIndex = -1;
}
// excluded getter/setters + basic error checking
public void Add()
{
try
{
if (maxIndex >= meetings.Length - 1)
{
throw new ApplicationException("YOU CAN ONLY CREATE 10 MEETINGS");
}
else
{
maxIndex++;
currentIndex = maxIndex;
Meeting temp = new Meeting(Title, Location, StartTime, EndTime, Notes);
meetings[maxIndex] = temp;
Title = meetings[maxIndex].Title;
Location = meetings[maxIndex].Location;
StartTime = meetings[maxIndex].StartTime;
EndTime = meetings[maxIndex].EndTime;
Notes = meetings[maxIndex].Notes;
}
}
catch (ApplicationException ex)
{
throw ex; // toss it up to the presentation
}
}
public void Add(string title, string location, string startTime, string endTime, string notes)
{
try
{
Title = title;
Location = location;
StartTime = startTime;
EndTime = endTime;
Notes = notes;
Add();
}
catch (ApplicationException ex)
{
throw ex;
}
}
public override string ToString()
{
return Title;
}
}
public partial class CalendarForm : Form
{
private MeetingManager mManager; // reference to business layer object
private void calendarSaveChangesButton_Click(object sender, EventArgs e)
{
try
{
mManager.Title = textBoxTitle.Text;
mManager.Location = textBoxLocation.Text;
mManager.StartTime = maskedStartTimeTextBox.Text;
mManager.EndTime = maskedEndTimeTextBox.Text;
mManager.Notes = notesTextBox.Text;
mManager.Add();
meetingListBox.Enabled = true;
meetingListBox.Items.Add(mManager);
//clears the textBoxes after clickng saveChanges
textBoxTitle.Text = "";
textBoxLocation.Text = "";
maskedStartTimeTextBox.Text = "";
maskedEndTimeTextBox.Text = "";
notesTextBox.Text = "";
}
catch (ApplicationException ex)
{
MessageBox.Show(this, ex.Message);
}
}
/// <summary>
/// When a meeting is selected from the listBox, it re-populates
/// the empty fields with the information stored in the array element
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void meetingListBox_SelectedIndexChanged(object sender, EventArgs e)
{
MeetingManager m = meetingListBox.SelectedItem as MeetingManager;
if (m != null)
{
textBoxTitle.Text = m.Title;
textBoxLocation.Text = m.Location;
maskedStartTimeTextBox.Text = m.StartTime;
maskedEndTimeTextBox.Text = m.EndTime;
notesTextBox.Text = m.Notes;
}
}
}