Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the selected date of a MonthCalendar control in C#

Tags:

c#

winforms

How to get the selected date of a MonthCalendar control in C# (Window forms)

like image 368
yonan2236 Avatar asked Aug 07 '10 04:08

yonan2236


3 Answers

"Just set the MaxSelectionCount to 1 so that users cannot select more than one day. Then in the SelectionRange.Start.ToString(). There is nothing available to show the selection of only one day." - Justin Etheredge

From here.

like image 103
Michael Todd Avatar answered Oct 20 '22 19:10

Michael Todd


I just noticed that if you do:

monthCalendar1.SelectionRange.Start.ToShortDateString() 

you will get only the date (e.g. 1/25/2014) from a MonthCalendar control.

It's opposite to:

monthCalendar1.SelectionRange.Start.ToString()

//The OUTPUT will be (e.g. 1/25/2014 12:00:00 AM)

Because these MonthCalendar properties are of type DateTime. See the msdn and the methods available to convert to a String representation. Also this may help to convert from a String to a DateTime object where applicable.

like image 41
WhySoSerious Avatar answered Oct 20 '22 20:10

WhySoSerious


Using SelectionRange you will get the Start and End date.

private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
    var startDate = monthCalendar1.SelectionRange.Start.ToString("dd MMM yyyy");
    var endDate = monthCalendar1.SelectionRange.End.ToString("dd MMM yyyy");
}

If you want to update the maximum number of days that can be selected, then set MaxSelectionCount property. The default is 7.

// Only allow 21 days to be selected at the same time.
monthCalendar1.MaxSelectionCount = 21;
like image 30
Sender Avatar answered Oct 20 '22 18:10

Sender