How to black out specific days of the week (datepicker) [VB]

白昼怎懂夜的黑 提交于 2021-02-08 05:18:42

问题


I was wondering if it is possible to make particular days unavailable from the calendar (DatePicker), more specifically every Monday and Tuesday. I have found similar threads (How do create a DatePicker with only Sundays enabled? and Disable specific days of the week on jQuery UI datepicker) about blacking out dates, however, I have not been able to modify their code for my specific goal. I'm writing this application in VB.NET (WPF).

The functions I used so far, for blacking out dates are:

Datepicker1.BlackoutDates.AddDatesInPast()
Datepicker2.BlackoutDates.Add(New CalendarDateRange(DateTime.Now.AddDays(1), DateTime.MaxValue))

Where the first function will blackout the past-dates, and the second will black out all future dates. Because there is a 'DateRange' required for the second function, I'm not able to alter this function for my need.

Thanks in advance

Jerry


回答1:


I modified one of the examples and came up with this.
It worked for me.

    private void MyDatePicker_CalendarOpened(object sender, RoutedEventArgs e)
    {
        MyDatePicker.DisplayDateStart = DateTime.Now;
        MyDatePicker.DisplayDateEnd = DateTime.Now + TimeSpan.FromDays(1000);

        var minDate = MyDatePicker.DisplayDateStart ?? DateTime.MinValue;
        var maxDate = MyDatePicker.DisplayDateEnd ?? DateTime.MaxValue;

        for (var d = minDate; d <= maxDate &&  DateTime.MaxValue >  d; d = d.AddDays(1))
        {
            if (d.DayOfWeek == DayOfWeek.Monday || d.DayOfWeek == DayOfWeek.Tuesday)
            {
                MyDatePicker.BlackoutDates.Add(new CalendarDateRange(d));
            }
        }
    }

And here's a bonus: Prevent Certain Dates from Being Selected.




回答2:


Thank you Okuma Scott, that was some helpful feedback! I rewrote your bit of code to VB language and according to my specific needs.

The included code will check all days in the next year, and will black out all the Mondays and Tuesdays.

Private Sub Datepicker_CalendarOpened(sender As Object, e As RoutedEventArgs) Handles Datepicker.CalendarOpened
        Dim currDate As Date = DateTime.Now
        Dim maxDate As Date = DateTime.Now.AddDays(356)

    While (currDate < maxDate)
        If currDate.DayOfWeek = DayOfWeek.Monday Or currDate.DayOfWeek = DayOfWeek.Tuesday Then
            DatumSelectie.BlackoutDates.Add(New CalendarDateRange(currDate))
        End If
        currDate = currDate.AddDays(1)
    End While

End Sub


来源:https://stackoverflow.com/questions/24213801/how-to-black-out-specific-days-of-the-week-datepicker-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!