0

いくつかの異なる情報(タイトル、場所、日付(monthCalendarから)など)のユーザー入力を受け入れるフォームを作成しました。[追加]ボタンをクリックすると、情報が現在の配列要素とタイトルに保存されます。 listBoxに表示されます。リストボックスのタイトルを選択すると、その特定のタイトルの残りの情報が適切なテキストボックスに再入力されます。

私はこれをさらに一歩進めようとしてきましたが、成功しませんでした。[追加]ボタンをクリックしたときに、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;

        }
    }

}

4

1 に答える 1

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
        }
    }

    ...

}
于 2013-03-02T22:57:42.917 に答える