Results 1 to 16 of 16

Thread: [RESOLVED] DateTimePicker packed a pickle of peppers

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Resolved [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?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: DateTimePicker packed a pickle of peppers

    Set the Value property like this:
    VB.NET Code:
    1. 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:
    1. 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    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

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: DateTimePicker packed a pickle of peppers

    This should work also.
    Code:
    myPicker.Value = Convert.ToDateTime("01/26/2009 8:00:00 AM")
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    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.

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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()
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. Datetimepicker1.Value = GetStartDate()

    EDIT: here's an example of how the GetStartDate function might look just to clarify what I mean:
    vb Code:
    1. Private Function GetStartDate As DateTime
    2.    Dim SelectCommand As New SqlCommand("SELECT StartDate FROM SomeTableName WHERE RowID=1",MySqlConnection)
    3.    Dim Result As DateTime
    4.  
    5.    MySqlConnection.Open
    6.    Result = SelectCommand.ExecuteScalar()
    7.    MySqlConnection.Close
    8.    Return Result
    9. 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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    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

  12. #12
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: DateTimePicker packed a pickle of peppers

    Quote 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?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  13. #13
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: DateTimePicker packed a pickle of peppers

    Quote 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  14. #14
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: DateTimePicker packed a pickle of peppers

    Quote 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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    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
  •  



Click Here to Expand Forum to Full Width