我创建了一个表单,接受几个不同信息(标题,位置,日期(来自月份日历)等)的用户输入,当点击添加按钮时,信息存储在当前数组元素中标题显示在listBox中。选择listBox中的Title后,将在相应的textBox中重新填充该特定Title的其余信息。
我一直在努力让这一步更进一步,但没有成功。单击“添加”按钮时,我希望将用户输入保存到monthCalendar上选择的日期。因此,如果用户单击没有保存信息的日期,则listBox保持为空。如果日期中保存了信息,则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;
}
}
}
答案 0 :(得分:0)
好吧,我想你可以尝试这样的事情:
class MeetingManager
{
...
//add and implement a find method which returns a Meeting-object if there is a
//corresponding meeting date (in private Meeting[] meetings;)
public Meeting MeetingFinder(DateTime meetingTime)
{
//if there is a corresponding meeting-object for the date, return the meeting object
//if there isn't, return null
}
...
}
public partial class CalendarForm : Form
{
...
private void monthCalendar_DateChanged(object sender, DateRangeEventArgs e)
{
//which date was selected?
var selectedDate = monthCalendar.SelectionRange.Start;
//do we have that date in the meetings?
var meetingOnTheSelectedDate = mManager.MeetingFinder(selectedDate);
if(meetingOnTheSelectedDate != null)
{
//populate your winform with the data from meetingOnTheSelectedDate
}
}
...
}