Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I use DateTimePicker under Window Form in C# programming.

When i click DateTimePicker, i want to display today onwards date only.

I mean that if today is 4 Dec 2013 , When i click DateTimePicker, only want to display 4 Dec 2013 onwards only(don't want to display before 4 Dec 2013).

Please give me advice how to do ?


Thanks

In same form, After i choose Date in DateTimePicker, i want to change back to original date used by click Button (RESET).
How to do ?
Please give me advice.
Posted
Updated 4-Dec-13 2:59am
v4
Comments
Philippe Mori 4-Dec-13 23:06pm    
You should read the documentation before posting a question... As other point out, there is a MinDate property and from that it should be easy to figure out what to do...

Try this,
dateTimePicker1.MinDate = DateTime.Today;
in Form_Load event.
 
Share this answer
 
Comments
Member 10404120 4-Dec-13 9:57am    
But i have 2 DateTimePicker; When I click first DateTimePicker and choose date(eg.04/12/13). I click second DateTimePicker , I only want to display 04/12/13 onwards only (dont want to display before 04/12/13).
Sayan Bera 4-Dec-13 10:05am    
As far i understand, the first datetimePicker will display normally and you choose a date(say X), and second one will display dates starting from X.

subscribe a event handler(dtp1_ValueChanged) for datatimepicker1's ValueChanged event and try the below code

private void dtp1_ValueChanged(object sender, EventArgs e)
{
//get the value from first datetimepicker
DateTime dt = dateTimePicker1.Value;
//apply the datetime to second one
dateTimePicker2.MinDate = dt;
}
Member 10404120 4-Dec-13 10:36am    
Thanks a lot ...
but i want to display in first datetimePicker from today date and choose date and second one will display dates starting from i chose date.
Sayan Bera 4-Dec-13 10:40am    
Okay, so in Form_load event set the mindate for datetimepicker1 to DateTime.today and then handle the event as I gave the code above.
There is MinDate property.

You need to set it to today:

dateTimeOicker1.MinDate = DateTime.Now;
 
Share this answer
 
Comments
Karthik_Mahalingam 3-Dec-13 11:30am    
this will work fine..
ridoy 3-Dec-13 12:04pm    
5ed!
Member 10404120 4-Dec-13 9:56am    
But i have 2 DateTimePicker;
When I click first DateTimePicker and choose date(eg.04/12/13).
I click second DateTimePicker , I only want to display 04/12/13 onwards only (dont want to display before 04/12/13).
Adam Zgagacz 4-Dec-13 10:00am    
So when you select value in first DateTimePicker set MidDate in second DateTimePicker to value you just selected.
Member 10404120 4-Dec-13 10:40am    
I want to display in first datetimePicker from today date and then i choose date
and second one will display dates starting from i choose date.
I hesitated to reply with solution for your questions from your comments, since you really should do your homework first. You need to study a little on your own.

But anyway here it goes your solution:

C#
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    dateTimePicker2.MinDate = dateTimePicker1.Value;
}



But please try to learn some basics first. This way you will not face problem in every other line of the code you are trying to write. Good luck with it:)
 
Share this answer
 
You need to set the min date of that datetime picker to todays date.

dateTimePicker1.MinDate = DateTime.Now;


Best of Luck.
 
Share this answer
 
Comments
Member 10404120 4-Dec-13 9:57am    
But i have 2 DateTimePicker; When I click first DateTimePicker and choose date(eg.04/12/13). I click second DateTimePicker , I only want to display 04/12/13 onwards only (dont want to display before 04/12/13).
C@dER@j 6-Dec-13 1:10am    
then on selected date changed event of the 1st datetimepicker get the date and this date set as the minimum date for the next datetime picker.

try it.
Philippe Mori 4-Dec-13 23:09pm    
It is probably better to set the minimum a midnight by using the appropriate property (Today or Date)
Have you thought about setting the DateTimePicker.Value?

e.g

C#
DateTimePicker.Value = DateTime.Now;



you could set this on the either the form constructor or the form load event
 
Share this answer
 
Comments
Member 10404120 4-Dec-13 9:57am    
But i have 2 DateTimePicker; When I click first DateTimePicker and choose date(eg.04/12/13). I click second DateTimePicker , I only want to display 04/12/13 onwards only (dont want to display before 04/12/13).
Simon_Whale 5-Dec-13 4:05am    
In that case I would use the TextChanged event of the datetimepicker to set the other datetimepickers minvalue (as this wont let you select a date before the value you set).

If you want to reset the date, just reset the min value to todays date i.e. datetime.now, but you will also have to set the value to the datetime.now also.
Philippe Mori 4-Dec-13 23:07pm    
You are not really responding to the question and as far as I remember this is what the control display anyway if the date is not explicitly set.
Simon_Whale 5-Dec-13 4:06am    
Apologies for not answering the question sooner, I was at work.

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