-2

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.

software is fun
  • 7,286
  • 18
  • 71
  • 129

1 Answers1

1

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.

Wheels73
  • 2,850
  • 1
  • 11
  • 20
  • Thanks. I am using monthCalendar and there is no event for ValueChanged. I will see what the equivalent event is. Oddly this fails when you select a date whose monday occurs in the previous month (I selected 2/3/17 and boom crash) – software is fun Feb 16 '17 at 17:08