Alish Safarli
Alish Safarli

Reputation: 65

How can I limit DatetimePicker in windows form c#?

I have two datetimePickers in my form.

I want to limit second datetimepicker to the end of the month of the year. If we are in 2020, its range should be till 31.12.2020.

private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker1.MinDate=?
        }

Upvotes: 2

Views: 449

Answers (2)

Cyber Progs
Cyber Progs

Reputation: 3873

You can use something like this

var maxDate = new DateTime(DateTime.Now.Year,12,31);

Upvotes: 0

Shahid Manzoor Bhat
Shahid Manzoor Bhat

Reputation: 1335

You should do something like this. There is no special event for setting the MaxDate you set it once you create an instance of DateTimePicker.

Also you should be setting datepicker'sMaxDate property not MinDate

DateTimePicker dateTimePicker2 = new DateTimePicker();

// Set the MaxDate.
dateTimePicker2.MaxDate= new DateTime(DateTime.Now.Year,12,31);

DateTimePicker has a Value Changed event which is called once you change any dates on the datepicker

Upvotes: 1

Related Questions