Search code examples
c#datetimedatetimepickernumericupdown

Use NumericUpDown to select days in month


Windows form application with a NumericUpDown and a DateTimePicker. Using the DateTimePicker to allow the user to select a time (21.20) I want to use the numericUpDown to allow the user to select a day.

How do I set the numericUpDown to only allow the user to select a valid day based on the number of days in the current month?


Solution

  • Use the DateTime.DaysInMonth() function to set the Maximum() Property of your NumericUpDown() in the Load() Event of the Form:

        private void Form1_Load(object sender, EventArgs e)
        {
            numericUpDown1.Maximum = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
        }