DateTimePicker and MonthCalendar

Two controls take a date. DateTimePicker is a field: the user types or picks one date, and a drop-down calendar appears on the arrow. MonthCalendar is a visible calendar: it shows a month, one date or a range of dates can be selected, and it takes more room than a form usually has. The first is the field a form has, the second is the field a booking form has.

DateTimePicker

using System; using System.Drawing; using System.Windows.Forms; internal static class Program { [STAThread] private static void Main() { ApplicationConfiguration.Initialize(); Application.Run(new Form1()); } } internal sealed class Form1 : Form { private readonly DateTimePicker _from = new() { Location = new Point(70, 16), Width = 140, Format = DateTimePickerFormat.Short, MinDate = new DateTime(2020, 1, 1), MaxDate = new DateTime(2030, 12, 31), Value = new DateTime(2026, 11, 21), }; private readonly DateTimePicker _remind = new() { Location = new Point(70, 54), Width = 140, Format = DateTimePickerFormat.Custom, CustomFormat = "ddd dd MMM yyyy", ShowCheckBox = true, Checked = false, }; private readonly Label _status = new() { Location = new Point(16, 96), AutoSize = true }; public Form1() { Text = "Dates"; ClientSize = new Size(360, 150); var fromLabel = new Label { Text = "&From:", Location = new Point(16, 20), AutoSize = true }; var remindLabel = new Label { Text = "&Remind:", Location = new Point(16, 58), AutoSize = true }; _from.ValueChanged += Describe; _remind.ValueChanged += Describe; Controls.Add(fromLabel); Controls.Add(remindLabel); Controls.Add(_from); Controls.Add(_remind); Controls.Add(_status); Describe(this, EventArgs.Empty); } private void Describe(object? sender, EventArgs e) { var from = _from.Value.ToString("yyyy-MM-dd"); var remind = _remind.Checked ? _remind.Value.ToString("yyyy-MM-dd") : "none"; _status.Text = $"from {from}, remind {remind}"; } }

dotnet build app.csproj

Build succeeded.
    0 Warning(s)
    0 Error(s)

The listing is built and not run. MinDate and MaxDate do for a date what Minimum and Maximum do for a number: the calendar greys the days outside the range and a typed date outside it becomes the nearest bound. ShowCheckBox = true adds a check box to the field, which is how a date can be empty: Checked is false and Value is not the answer until the user ticks it.

The time part is real. A picker with a date format shows no time and still holds one, so a value that comes back from Value is DateTime.Now when the user picked today, not midnight. A form that compares dates compares .Date, as the record in the last section does.

Formats

A picker whose format shows a time is the time control of WinForms; there is no separate one. ShowUpDown = true replaces the drop-down calendar with two spinner arrows, which is what a time field looks like.

MonthCalendar

using System; using System.Drawing; using System.Windows.Forms; internal static class Program { [STAThread] private static void Main() { ApplicationConfiguration.Initialize(); Application.Run(new Form1()); } } internal sealed class Form1 : Form { private readonly MonthCalendar _calendar = new() { Location = new Point(16, 16), MaxSelectionCount = 7, ShowToday = true, ShowWeekNumbers = false, }; private readonly Label _status = new() { Location = new Point(16, 190), AutoSize = true }; public Form1() { Text = "A week"; ClientSize = new Size(260, 240); _calendar.SetDate(new DateTime(2026, 11, 21)); _calendar.DateChanged += Describe; Controls.Add(_calendar); Controls.Add(_status); Describe(this, new DateRangeEventArgs(_calendar.SelectionStart, _calendar.SelectionEnd)); } private void Describe(object? sender, DateRangeEventArgs e) { var start = e.Start.ToString("yyyy-MM-dd"); var end = e.End.ToString("yyyy-MM-dd"); _status.Text = $"{start} to {end}"; } }

dotnet build app.csproj

Build succeeded.
    0 Warning(s)
    0 Error(s)

The listing is built and not run. MaxSelectionCount = 7 is the rule that turns a calendar into a week picker: the user drags across days and the control refuses to extend the range past seven. SelectionStart and SelectionEnd are the range, SetDate moves it from code, and DateChanged reports a new range with a DateRangeEventArgs. DateSelected is the other event and fires when the user picks a single day, before the range settles.

A MonthCalendar takes the space its squares need; a form that must stay small uses two DateTimePicker fields and does the range check itself, which is the next section.

A date range that runs

A range has rules whether or not a calendar enforces them: the end comes after the start, and the booking is not longer than a week. That is a value type and a method, and it runs here:

using System; using System.Globalization; var week = new DateRange(new DateTime(2026, 11, 21), new DateTime(2026, 11, 27)); var month = new DateRange(new DateTime(2026, 11, 1), new DateTime(2026, 11, 30)); var reversed = new DateRange(new DateTime(2026, 11, 27), new DateTime(2026, 11, 21)); foreach (var range in new[] { week, month, reversed }) { var ok = range.IsValid(out var error); var note = ok ? string.Empty : $" ({error})"; Console.WriteLine($"{range.Start:yyyy-MM-dd} to {range.End:yyyy-MM-dd}: {range.Days} days, valid={ok}{note}"); } Console.WriteLine($"picker text: {week.Start.ToString("d", CultureInfo.InvariantCulture)}"); internal readonly record struct DateRange { public DateRange(DateTime start, DateTime end) { Start = start.Date; End = end.Date; } public DateTime Start { get; } public DateTime End { get; } public int Days => (End - Start).Days + 1; public bool IsValid(out string error) { if (End < Start) { error = "the end is before the start"; return false; } if (Days > 7) { error = "a week at most"; return false; } error = string.Empty; return true; } }

dotnet run

2026-11-21 to 2026-11-27: 7 days, valid=True
2026-11-01 to 2026-11-30: 30 days, valid=False (a week at most)
2026-11-27 to 2026-11-21: -5 days, valid=False (the end is before the start)
picker text: 11/21/2026

The output is the real run, and three details are worth keeping. The constructor takes .Date of both ends, so a range built from DateTime.Now has no time of day in it. A reversed range has a negative day count, which is why the validity check comes first and the count is only meaningful after it. And the last line shows the culture dependency of every date the control shows: "d" in the invariant culture is 11/21/2026, and the same value on a machine with another culture is another string.

Exercises

  1. Add a third picker with Format.Time and ShowUpDown, and print the value it would hold.
  2. Make IsValid also refuse a start date in the past, and run the program with a past start.
  3. Print the week range with a German culture and compare it with the invariant line.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which property holds the date of a DateTimePicker?

    Answer

    Value, a DateTime

    Value is the DateTime; Text is only the formatted display.

  2. Which format shows the pattern in CustomFormat?

    Answer

    DateTimePickerFormat.Custom

    Custom is the one that reads CustomFormat.

  3. How does a DateTimePicker represent an empty date?

    Answer

    With ShowCheckBox = true and Checked = false

    The check box is the control's way of saying that no date was chosen.

  4. What do MinDate and MaxDate do?

    Answer

    Bound the calendar and clamp a typed date to the nearest bound

    The days outside the range are greyed and cannot be chosen.

  5. What does a picker with a date format hold in the time part of Value?

    Answer

    A real time, often the time the value was created

    The format hides the time; it does not remove it, which is why dates are compared as .Date.

  6. Which property limits how many days a MonthCalendar can select?

    Answer

    MaxSelectionCount

    It is the control's own limit, applied while the user drags.

  7. Which pair of properties is the selected range of a MonthCalendar?

    Answer

    SelectionStart and SelectionEnd

    SelectionRange wraps them; the two properties are the direct read.

  8. What did the run print for the reversed range?

    Answer

    -5 days, valid=False (the end is before the start)

    The subtraction gives a negative count and the validity check names the rule.

  9. Why does the DateRange constructor take .Date of both ends?

    Answer

    So two ranges of the same days compare equal whatever the time was

    A value built from DateTime.Now would otherwise carry a time that makes equality and day counts wrong.

  10. Which control shows a time of day?

    Answer

    A DateTimePicker with Format.Time or a custom time pattern

    There is no separate time control; the picker's time format is it.

Every listing on this page was built with the .NET SDK 10.0.303 for net10.0-windows against the targeting pack Microsoft.WindowsDesktop.App.Ref 10.0.15 from the offline feed, in a Docker container on Alpine Linux 3.24.3 (x86_64). The programs were built, not run on Linux: WinForms needs Windows. The logic that draws nothing was also run headless on the same machine, and its output is the run output printed below each of those programs.

Article author: Arthur Isaev