Search code examples
c#winformsmonthcalendar

How do I make every Monday Bold in the monthCalendar control?


I am developing an application. I initially wanted to allow the user to ONLY select a Monday. After spending a considerable amount of time, I found out that that wont be possible unless i create my own control.

Therefore i wanted to know how do I make Monday's Bold (and even change the background color) so it's more noticeable?

Is there a way to programatically select a Monday (of the same week) no matter what day is selected on the week?

For example, if they click 2/16, that 2/13 would automatically be selected.


Solution

  • I've done this on a datetime picker value changed event. Seems to work. Hope it helps!

      private void dtP1_ValueChanged(object sender, EventArgs e)
            {
                var days = DayOfWeek.Monday - dtP1.Value.DayOfWeek;
    
                if (dtP1.Value.DayOfWeek != DayOfWeek.Monday)
                {
                    dtP1.Value = new DateTime(dtP1.Value.Year, dtP1.Value.Month, dtP1.Value.Day + days);
                }
    
            }
    

    I've managed to write and extension method for it.

     public static class DateTimeHelper
        {
            public static void AlwaysChooseMonday(this DateTimePicker dtp, DateTime value)
            {
                var days = DayOfWeek.Monday - dtp.Value.DayOfWeek;
    
                if (dtp.Value.DayOfWeek != DayOfWeek.Monday)
                {
                    dtp.Value = new DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day + days);
                }
            }
        }
    

    Then the value changed event just becomes

     private void dtp1(object sender, EventArgs e)
            {
                 dtP1.AlwaysChooseMonday(dtP1.Value);
            }
    

    a lot neater.