How in C # to block dates in DateTimePicker every 7 days from the beginning of the year and all holidays?

  • WPF or WinForms? - VladD
  • @VladD WinForms - Sergey74rus
  • probably worth clarifying what kind of "holidays" are implied. - aleksandr barakin
  • one
    @ Sergey74rus I don’t think this is possible in WinForms with standard DateTimePicker . - SᴇM
  • EnSO also says that it is impossible and recommend using a similar component from WPF or DevExpress. stackoverflow.com/a/2364096/5796587 , stackoverflow.com/a/27319756/5796587 - rdorn

1 answer 1

Something like this. Holidays add handles. And Jan. + 7days is not very good. Probably wanted all the sun. Well, then we must also determine the first sun of the year.

 public partial class Form1 : Form { private readonly List<DateTime> _blockedDates = new List<DateTime>(); // List of blocked dates private DateTime Dt { get; set; } public Form1() { InitializeComponent(); Dt=DateTime.Now; var d= new DateTime(DateTime.Now.Year,1,1); //Fill blocked dates with 1/1 + each 7 days while (d.Year == DateTime.Now.Year) { _blockedDates.Add(d); d=d.AddDays(7); } } private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { DateTimePicker ctl = (sender as DateTimePicker); if (ctl == null) return; if (BlockedDate(ctl.Value))ctl.Value = Dt; else Dt = ctl.Value; } private bool BlockedDate(DateTime value) { return _blockedDates.Any(x => x.Date == value.Date); } }