开发者

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

开发者 https://www.devze.com 2023-01-10 09:51 出处:网络
How to get the selected date of a MonthCalendar control i开发者_StackOverflow社区n C# (Window forms)\"Just set the MaxSelectionCount to 1 so that users cannot select more than one day. Then in the Sel

How to get the selected date of a MonthCalendar control i开发者_StackOverflow社区n C# (Window forms)


"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.


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.


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;


For those who are still trying, this link helped me out, too; it just puts it all together:

http://dotnetslackers.com/VB_NET/re-36138_How_To_Get_Selected_Date_from_MonthCalendar_control.aspx

private void MonthCalendar1_DateChanged(object sender, System.Windows.Forms.DateRangeEventArgs e)
{
//Display the dates for selected range
Label1.Text = "Dates Selected from :" + (MonthCalendar1.SelectionRange.Start() + " to " + MonthCalendar1.SelectionRange.End);

//To display single selected of date
//MonthCalendar1.MaxSelectionCount = 1;

//To display single selected of date use MonthCalendar1.SelectionRange.Start/ MonthCalendarSelectionRange.End
Label2.Text = "Date Selected :" + MonthCalendar1.SelectionRange.Start;
}


It'll be helpful if you want just to convert it by:

String myCalendar = monthCalendar1.SelectionRange.Start.ToShortDateString() 

But if you want to get a formatted output you could instead:

String myCalendar = monthCalendar1.SelectionRange.Start.ToString("yyyy-MM-dd")

It's important to use year and day as lower caps, and month as upper or else it'll return you a wrong format, for example, if you do:

String myCalendar = monthCalendar1.SelectionRange.Start.ToString("YYYY-MM-DD")

it will return: YYYY-07-DD (If the original date's month was July)


private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {
    string clickeddate = monthCalendar1.SelectionRange.Start.ToString("dddd, dd MMM yyyy");
    richTextBox.AppendText(clickeddate); //or whatever you decide to do with it. 
    }


SelectionRange property

0

精彩评论

暂无评论...
验证码 换一张
取 消