Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!

I just have a quick question for everyone. Is it possible to disable some of the months in a DateTimePicker? I'm doing this program that accepts a date range. However it's maximum range is 6 months. For example, if i choose July in DateTimePicker1, the values in DateTimePicker2 should only be August, September, October, November, December. How will i disable the months January, February, March, April, May and June? Thanks!

x
Janna
Posted

1 solution

Something like this would help you:

VB
DateTimePicker1.MinDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
DateTimePicker1.MaxDate = DateTimePicker1.MinDate.AddMonths(6)


This code will restrict the control to show only the current month and the upcoming 6 months. You can pass first DateTimePicker's month in MinDate .
 
Share this answer
 
v3
Comments
Janna Dela Cruz 11-Oct-12 20:57pm    
Thanks! It works!
Janna Dela Cruz 11-Oct-12 23:42pm    
Sir Shahan Ayyub,
May you please help me again? I got the date range the next thing i have to do is print the days of the months that was chosen, i have a running program but it only print first day of the chosen month. I tried to use the System.DateTime.DaysInMonth but i'm having an error. Do you know how will i fix that? Thank you very much.
Shahan Ayyub 12-Oct-12 1:31am    
What error did you get ?
Have you tried something like this :

Dim daysInMonth As Integer = DateTime.DaysInMonth(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month)

It will print the days in month from the date chosen from datetimepicker1 control.
Janna Dela Cruz 12-Oct-12 1:49am    
Actually, that's what I'm using.

This is the error "ArgumentOutOfRangeException was unhandled : Year, Month and Day parameters describe an un-representable DateTime"

I think it has something to do with the number of days in a month. Because the error comes whenever i used months that has 31 days, as well as the 28/29.
Shahan Ayyub 12-Oct-12 2:03am    
I have just re-checked the code it seems to working fine. Can you post your code ? Also, if you could provide few more lines other than the affected line. It would be great to troubleshoot the issue. Also, Can you tell us which date have you selected ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900