I am creating a form that uses a DateTimePicker control to display the date in the format of h:mm:ss tt. I am trying to make the default time one hour ahead.

The control is labeled deliveryTime and I tried double clicking it and then typing in
DateTime.Now.AddHours(1);

I dont know why it won't work. Here is my code which is another method I tried to input

private void deliveryTime_ValueChanged(object sender, EventArgs e)
      {
         deliveryTime.MinDate = DateTime.Now.AddHours(1);
      }

Any suggestions would be great. Thanks

Recommended Answers

All 4 Replies

First of all, DateTimePicker have nothing to do with DateTime they are two seperate things.
Second, the problem is in your method,you see you have a method(deliveryTime_ValueChanged) and when this method is called or triggered it'll run the code inside of it (deliveryTime.MinDate = DateTime.Now.AddHours(1);) and the problem is that there's nothing to trigger your method which is changing the date of your dateTimePicker, it's like you want to see what's inside of a potato chips bag without opening the bag.
i recommend that you use a timer tick event like so:

private void timer1_Tick(object sender, EventArgs e)
{
   deliveryTime.Mindate = DateTime.Now.AddHours(1);
}

add a timer control and you can call it by Timer1.enabled = true;

@oussama_1
You first suggestions are correct.
The suggestion of using a timer is not. What if the timer ticks at say every second? The delivery time would augment with an extra hour etc.
I should use the ValueChanged event of the DateTimePicker to add an hour to the other control.

@ddanbe
It's impossible that this code adds an extra hours other than one hour to the current time (meaning whatever time it is, it'll only add only one hour to it), and i recommended the timer tick event other than ValueChanged event for one important thing, that is he want to show the seconds in the control
so the control needs to change every second to display a proper time.
i'm sorry i didn't clear that out in my first post.

the date in the format of h:mm:ss tt

commented: Good point. +15

Weel, I must admit you got a point there. :)

commented: thank you +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.