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

后端 未结 5 694
我寻月下人不归
我寻月下人不归 2020-12-28 13:46

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

相关标签:
5条回答
  • 2020-12-28 13:55

    SelectionRange property

    0 讨论(0)
  • 2020-12-28 13:57

    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;
    
    0 讨论(0)
  • 2020-12-28 13:58

    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;
    }
    
    0 讨论(0)
  • 2020-12-28 14:07

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

    0 讨论(0)
  • 2020-12-28 14:10

    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.

    0 讨论(0)
提交回复
热议问题