Results 1 to 2 of 2

Thread: code to input date-time using InputBox

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2011
    Location
    Melbourne
    Posts
    37

    code to input date-time using InputBox

    Here is my code to input date-time using InputBox
    with default date-time = today at 9 pm
    I thought others might be interested or suggest improvements
    vb.net Code:
    1. Dim KeepOn As Boolean
    2.         Dim DateDefault As Date
    3.         Dim strDate As String
    4.         Dim Date1 As DateTime
    5.         Dim MyDateFmt As String = "dd/MM/yyy HH:mm:ss"
    6.  
    7.         DateDefault = DateAdd("h", 21, Today())
    8.        ' Today 00 hours plus 21 hours
    9.         KeepOn = True
    10.  
    11.         While KeepOn
    12.             strDate = InputBox(" Enter date with format " + MyDateFmt, , _
    13.                                Format(DateDefault, MyDateFmt))
    14.  
    15.             Try
    16.                 Date1 = DateTime.Parse(strDate)
    17.                 KeepOn = False
    18.                 MsgBox(" No error detected in date.parse.  Proceed"  _
    19.                            + Date1.ToString)
    20.                    
    21.             Catch ex As Exception
    22.                 MessageBox.Show("Exception in Date.Parse" + _
    23.                            vbNewLine + " Date string = " + strDate + _
    24.                            vbNewLine + "Exception Type: " _
    25.                             + ex.GetType.ToString + _
    26.                            vbNewLine + "Exception Message: "  _
    27.                            + ex.Message, "Exception", _
    28.                            MessageBoxButtons.OK, MessageBoxIcon.Error)
    29.                 KeepOn = True
    30.             End Try
    31.  
    32.         End While
    Last edited by Hack; Dec 15th, 2011 at 08:26 AM. Reason: Added Highlight Tags

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

    Re: code to input date-time using InputBox

    My first advice would to never use InputBox and, if you want the user to enter a date and/or time, display your own dialogue with a DateTimePicker control.

    Next, using Date.Parse inside a Try...Catch block is bad practice. When you don't know that the input will be valid you should be using TryParse, which doesn't throw an exception if it fails.

    Finally, if you're expecting input in a specific format, as you are, then you need to enforce that format. To do that you use ParseExact in preference to Parse and TryParseExact in preference to TryParse.

    The less critical issues involve getting rid of all those Runtime and using .NET standards.
    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

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