Question

I have a MonthCalendar and a Datetimepicker in a form. Is there a way to synchronize them? In other words, when I change MonthCalendar' s date, the date of DateTimePicker will be automatically equal to that date and the opposite. I ' ve tried these below, but both don't work.

datetimePicker1 = monthCalendar1.SelectionRange.ToString("dd MMMM yyyy  hh :mm");
monthCalendar1 = datetimePicker1.Value.ToOADate();
Was it helpful?

Solution

Just use the default events of these controls to have one update the other. Double-click the controls to add the event handlers and make them look like this:

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
        monthCalendar1.SetDate(dateTimePicker1.Value);
    }

    private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e) {
        dateTimePicker1.Value = monthCalendar1.SelectionStart;
    }

Do note that DateTimePicker already has a MonthCalendar built-in, you get it when you click the dropdown button. Which is the typical way it is used, they are already in sync.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top