1

Is it possible to set the timepicker value in code.

What I am thinking here is that if I have a timepicker and choose a time value, thats the time is should use.

But if I dont choose a time value, it should set 00:00 as default and I want to do this with code.

Hade some crazy idea that looked like this:

DateTime date;
date.Hour(00:00:00)

Didnt work that good so is there any good way for making this.

UPDATE

Here is what i am thinking:

DateTime? temp = (DateTime)startDate.Value;
            DateTime date1;
            if (temp == null)
            {
               //Set the time to 00:00
            }
            else
            {
                date1 = (DateTime)temp;
            }

update2 Here is all code and getting an exception and thats why i want to set the value in code becasue accepting nullable datetime didnt seem to work.

  DateTime date = (DateTime)datePicker.Value;
  DateTime break1S = (DateTime)startBreak1.Value;
   _nestedDateStartBreak1 = new DateTime(date.Year, date.Month, date.Day, break1S.Hour, break1S.Minute, 0);
codemoonger
  • 613
  • 3
  • 11
  • 22
  • What is the exception? – Evan L Jun 03 '13 at 18:35
  • check my other post about this exception here http://stackoverflow.com/questions/16893855/exception-of-type-system-invalidoperationexception-occurred-in-mscorlib-ni-dll – codemoonger Jun 03 '13 at 18:42
  • is it not ok to have two post about almost the same thing – codemoonger Jun 03 '13 at 19:04
  • It's okay, just disorganized. Had i known you had a seperate post it would have helped formulate a better answer. I commented on the other post to help direct people to this post if they are also confused. Will take a closer look later. – Evan L Jun 03 '13 at 19:08

1 Answers1

0

If you have already overridden the DateTimePicker control to be only a TimePicker you just need to set the text property of the control.

    myTimePicker.Text = "00:00:00";

If you still have a DateTimePicker and want to make it only a TimePicker just set the following properties:

    myTimePicker.Format = DateTimePickerFormat.Time;
    myTimePicker.ShowUpDown = true;

My suggestion is to set the default Text value ahead of time so that if the user does not input a time, it will contain the default time you specify already.

EDIT: Setting default value for your DateTime object.

    DateTime defaultDate = new DateTime(); 
    defaultDate = Convert.ToDateTime("2013-12-31 00:00:00"); //Or whatever you want your default value to be...
    if (startDate.Value == null) 
    { 
        startDate.Value = defaultDate; 
    } 
    else 
    { 
        //do something else with the value 
    }

Somthing like that should get you the results you want.

Evan L
  • 3,805
  • 1
  • 22
  • 31
  • Format and Text or DateTimePickerFormat, I cant set non of these – codemoonger Jun 03 '13 at 14:25
  • In your OP, you said you have a `timePicker` control. I don't see any code related to that in your example. Are you asking about a `DateTimePicker` control? Or a `DateTime` object? – Evan L Jun 03 '13 at 14:27
  • Okay, the code you posted is not a `DateTimePicker` but rather a `DateTime`. `Format` and `Text` are methods on a `DateTimePicker`. So i think you are just trying to manipulate the wrong object. Take a look at `startDate.Text`. And don't cast it to datetime. – Evan L Jun 03 '13 at 14:39
  • I added code that is more specifically aimed at your question. – Evan L Jun 03 '13 at 14:43
  • I am using it like this: _nestedDateStartBreak1 = new DateTime(date.Year, date.Month, date.Day, date1.Hour, date1.Minute, 0); so i need it to be datetime i gues – codemoonger Jun 03 '13 at 14:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31122/discussion-between-alexander-mogren-and-evan-lewis) – codemoonger Jun 03 '13 at 14:58
  • @AlexanderMogren I waited in chat for over an hour (while continuing with my work of course) ;) But you did not answer, so I updated my code with a more concise way to default your picker value. – Evan L Jun 03 '13 at 16:56
  • i am really sorry. didnt see you. – codemoonger Jun 03 '13 at 17:01
  • only need to set the time because the date is set from a datepicker – codemoonger Jun 03 '13 at 17:25
  • Sort of feels like you want me to write the code for you... I think with the info I've given you should be able to get it working. – Evan L Jun 03 '13 at 17:29
  • no i dont want you to write it. i am not sure that you understand what i want. so bad on explain. – codemoonger Jun 03 '13 at 17:30
  • If you are trying to ONLY set a time on a `DateTime` object, then it will be problematic. Check out the post here on the topic [LINK](http://stackoverflow.com/questions/2037283/how-do-i-represent-a-time-only-value-in-net) Otherwise, yes I apparently do not understand what you are asking. Perhaps clarify your question in the OP. – Evan L Jun 03 '13 at 17:33