|
-
Jan 26th, 2009, 03:16 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] DateTimePicker packed a pickle of peppers
Hi;
How do I assign a starting date and time to a DateTimePicker. I tried...
myPicker.text = "01/26/09 08:00" but the following error message is returned....
String was not recognized as a valid DateTime
What am I doing wrong?
-
Jan 26th, 2009, 03:21 PM
#2
Re: DateTimePicker packed a pickle of peppers
Set the Value property like this:
VB.NET Code:
myPicker.Value = New Date(2009, 1, 26, 8, 0, 0)
Setting the text property like you're trying to do is equivalent to doing this:
VB.NET Code:
myPicker.Value = DateTime.Parse("01/26/09 08:00", CultureInfo.CurrentCulture)
And the error you're getting would be because it was unable to parse your string as a proper DateTime.
-
Jan 26th, 2009, 03:38 PM
#3
Re: DateTimePicker packed a pickle of peppers
Code:
Dim s As String = "01/26/09 08:00"
If DateTime.TryParse(s, myPicker.Value) Then
'string was a valid datetime format
Debug.WriteLine("OK " & myPicker.Value.ToString)
Else
'bad datetime
Debug.WriteLine("BAD " & myPicker.Value.ToString)
End If
-
Jan 26th, 2009, 04:05 PM
#4
Lively Member
Re: DateTimePicker packed a pickle of peppers
***UNRELATED TO QUERY***
In answer to the age old question, "Where's the peck of pickled peppers Peter Piper picked?"
Peter Piper put the peck of pickled peppers in his pink and purple polyester pants pocket.
Anyone who does not wonder, is either omnipotent or a fool.
Amerigoware <<<My Projects
-
Jan 26th, 2009, 04:20 PM
#5
Re: DateTimePicker packed a pickle of peppers
Here's another way. You can create a date object by wrapping your date in pound signs rather than quotes.
Code:
myPicker.Value = #1/26/2009 8:00:00 AM#
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Jan 26th, 2009, 05:00 PM
#6
Re: DateTimePicker packed a pickle of peppers
This should work also.
Code:
myPicker.Value = Convert.ToDateTime("01/26/2009 8:00:00 AM")
-
Jan 26th, 2009, 08:03 PM
#7
Re: DateTimePicker packed a pickle of peppers
Why would you assign a literal in code anyway? If you want to assign a literal to the Value property of a DateTimePicker then would you not do so in the designer? If you need to do some calculation to get the value then you'd be using a DateTime value anyway, so this whole discussion becomes moot.
-
Jan 27th, 2009, 01:18 PM
#8
Thread Starter
Hyperactive Member
Re: DateTimePicker packed a pickle of peppers
dee-u
Your suggestion was about the easiest to implement however to see the result I have to add an extra line...
myPicker.focus
Any idea why this is so?
jmcilhinney
Why would you assign a literal in code anyway? I wouldn't, the code sample was to illustrate what I need to do.
I am retrieving a time from a database table and use this as an arbitrary start point for a DateTimePicker. The value showing in the DateTimePicker is today's date combined with the time value from the table.
-
Jan 27th, 2009, 01:42 PM
#9
Re: DateTimePicker packed a pickle of peppers
i vote for my idea if the datetime value changes. if it is a constant then i vote for jmc. "easiest to implement" scares me.
btw - don't use focus. if you need to give focus to a control use .Select
Code:
Dim s As String = "7/29/09 08:00"
myPicker.Format = DateTimePickerFormat.Custom
myPicker.CustomFormat = "MM/dd/yyyy hh:mm:ss tt"
If DateTime.TryParse(s, myPicker.Value) Then
'string was a valid datetime format
Debug.WriteLine("OK " & myPicker.Value.ToString)
Else
'bad datetime
Debug.WriteLine("BAD " & myPicker.Value.ToString)
End If
myPicker.Select()
-
Jan 27th, 2009, 01:51 PM
#10
Re: DateTimePicker packed a pickle of peppers
If you are recieving the date from a database then I assume the database field is of type DateTime (if its SQL or similar anyway). In which case then you could just set the value of the datetimerpicker to the value you got from the database directly by using the DateTime type like JMC said.
A very simple example (where GetStartDate is a function that returns a DateTime type object which you got from the databse):
vb.net Code:
Datetimepicker1.Value = GetStartDate()
EDIT: here's an example of how the GetStartDate function might look just to clarify what I mean:
vb Code:
Private Function GetStartDate As DateTime
Dim SelectCommand As New SqlCommand("SELECT StartDate FROM SomeTableName WHERE RowID=1",MySqlConnection)
Dim Result As DateTime
MySqlConnection.Open
Result = SelectCommand.ExecuteScalar()
MySqlConnection.Close
Return Result
End Function
Obviously thats very basic and assumes a few things (and its written from memory so might need some small corrections to run properly) but you get the idea
Last edited by chris128; Jan 27th, 2009 at 01:56 PM.
-
Jan 27th, 2009, 03:32 PM
#11
Thread Starter
Hyperactive Member
Re: DateTimePicker packed a pickle of peppers
Thanks guys for all the help. Your suggestions all work well but I have discovered that these two statements are required.
DTP_Start1.Format = DateTimePickerFormat.Custom
DTP_Start1.CustomFormat = "MM/dd/yy HH:mm"
I'm not sure why because I set them when I created the controls. However my control, DTP_Start1, will not display the date/time without them.
So here is what dee-u's code looks like in the working state
DTP_Start1.Format = DateTimePickerFormat.Custom
DTP_Start1.CustomFormat = "MM/dd/yy HH:mm"
DTP_Start1.Value = Convert.ToDateTime(Format(Now(), "MM/dd/yy") & " " & Format(myDbCommand.Parameters(3).Value, "HH:mm"))
dbasnett's code
s = Format(Now(), "MM/dd/yy") & " " & Format(myDbCommand.Parameters(3).Value, "HH:mm")
DTP_Start1.Format = DateTimePickerFormat.Custom
DTP_Start1.CustomFormat = "MM/dd/yy HH:mm"
DateTime.TryParse(s, DTP_Start1.Value)
What does myPicker.select() do?
I think JMC was curious about why I was coding a literal string and I think my explanation cleared it up.
Thanks for sharing your code chris128
-
Jan 27th, 2009, 03:51 PM
#12
Re: DateTimePicker packed a pickle of peppers
 Originally Posted by dbasnett
btw - don't use focus. if you need to give focus to a control use .Select
Control.Focus would focus on the control, why are you suggesting .Select?
-
Jan 27th, 2009, 04:19 PM
#13
Re: DateTimePicker packed a pickle of peppers
 Originally Posted by dee-u
Control.Focus would focus on the control, why are you suggesting .Select? 
You're looking in the ".NET Framework Class Library for Silverlight"-section of MSDN Here is the windows forms version of the Control.Focus documentation, which states:
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
-
Jan 27th, 2009, 04:53 PM
#14
Re: DateTimePicker packed a pickle of peppers
 Originally Posted by Atheist
You're looking in the ".NET Framework Class Library for Silverlight"-section of MSDN Here is the windows forms version of the Control.Focus documentation, which states:
Aahhh, I see. I am exclusively using Focus without problems but knowing about Select is nice. I wonder how one could demonstrate any repercussions on using Focus instead of Select.
-
Jan 27th, 2009, 05:06 PM
#15
Re: DateTimePicker packed a pickle of peppers
Yes, your explanation does clear it up. It shows us exactly what you're doing wrong. If you have two DateTime values to start with and you want a DateTime value at the end then there is no reason or point to using any strings at all:
Code:
date2 = Date.Today.Add(date1.TimeOfDay)
That will take today's date and add the time portion of another DateTime, then assign the result to a third DateTime, which is exactly what you want to do. NEVER use strings to represent dates and times unless it is for display or serialisation purposes.
-
Jan 28th, 2009, 11:33 AM
#16
Thread Starter
Hyperactive Member
Re: DateTimePicker packed a pickle of peppers
Hi jmcilhinney
I coded your suggestion last night and it works great. Thanks for taking to the time and effort to help with this.
Everyone's suggestions and advice are most appreciated.
Lin
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|