Question

I'm using the monthCalendar control from the toolbox in VS2010 and I am having difficulty in trying to make the calendars date "select-able". The date selected will be stored in an array with the rest of the input entered by the user. I felt that after a few hours of searching and finding results that did not relate to my problem that posting here would be the best solution. Any help is appreciated.

        int currentIndex;
        int arraySize = 0;
         Meeting[] meetingArray = new Meeting[100];

      private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {
        e.Start.ToShortDateString();
    }

    private void calendarSaveChangesButton_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);
    }
Was it helpful?

Solution

Go to the monthCalendar in your form and right click. Select Properties. Then click Events and double-click DateSelected.

This will be generated by VS2010:

private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{

}

Inside that method use e.Start.ToShortDateString() to capture the date that was selected and assign it to the appropriate index of your array.

Update in regards to your question in the comments/ newly posted code:

From what I can tell about your project, this might be the best solution for you (without having to completely restructure everything). Add another text box (name it dateBox). Then inside the DateSelected method add the following code:

dateBox.Text = e.Start.ToShortDateString();

Then inside your Click method that you have posted (assuming that your Meeting class has some sort of Date property) add the following code:

m.Date = dateBox.Text;

Update

Alternatively (and much more simply - I don't know why I didn't suggest this before), you could just define a global string in your form called dateStr and use that to hold the information for the date of the meeting until you click "save changes" and then assign it to m.Date. That would be a much more simpler solution than creating another textbox (which is probably what you're trying to avoid anyway).

OTHER TIPS

The MonthCalendar control has a few properties you should be able to access: SelectionRange, SelectionStart, and SelectionEnd that contains the values of the date(s) selected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top