3 Replies - 6778 Views - Last Post: 16 February 2012 - 10:58 PM Rate Topic: -----

#1 glenak1911   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 16-May 11

Visual Basic 2010 formatting user input as time?

Posted 16 February 2012 - 09:44 PM

Hi guys I'm trying to make a program that takes user input and sets it to a time value. So far I have:

Private Sub txtDepartTime_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtDepartTime.TextChanged
        'Get the time
        'departTime = txtDepartTime.Text
        'departTimeString = departTime.ToString(TimeOfDay)
        departTime = DateTime.Parse(txtDepartTime.Text)
        lblTest.Text = departTime
    End Sub



I'm getting an invalid cast error. Does anyone have any hints on how to format the user input as a time value?

Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic 2010 formatting user input as time?

#2 EtienneBordeaux   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 09-February 12

Re: Visual Basic 2010 formatting user input as time?

Posted 16 February 2012 - 10:30 PM

If you are using datetimepicker, try this one wherein the control datetimepicker was named as txtDepartTime.
txtDepartTime.Format = DateTimePickerFormat.Time
        txtDepartTime.ShowUpDown = True

Was This Post Helpful? 1
  • +
  • -

#3 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Visual Basic 2010 formatting user input as time?

Posted 16 February 2012 - 10:39 PM

If you can't control what the user enters then you need to use .TryParse to ensure that you can process the input properly. If TryParse returns False, then the entry cannot be converted to the type you're trying to parse at which point you notify the user of the error and allow them to fix it.

What type of variable is departTime?

Have you thought about using a DateTimePicker control to allow the user to enter time. It is nothing more than a simple format in the properties to give a more error-free entry for this type of user input.

This post has been edited by CharlieMay: 16 February 2012 - 10:39 PM

Was This Post Helpful? 1
  • +
  • -

#4 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Visual Basic 2010 formatting user input as time?

Posted 16 February 2012 - 10:58 PM

you can convert user input text with something like this. But as EtienneBordeaux and CharlieMay have mentioned above, using this is just overkill comparing to something simple as using a DateTimePicker. If it is essential, I'd suggest you to use a MaskedTextbox to set up an exact format for the user to input the Time because a simple textbox will definitely cause unexpected errors.

Imports System.Globalization.CultureInfo
Imports System.Globalization.DateTimeStyles

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim dateString As String = TextBox1.Text
        Dim format As String() = New String() {"HHmmss"}

        Dim dt As DateTime = DateTime.ParseExact(dateString, format, InvariantCulture, AdjustToUniversal)
        Dim timeString As String = dt.ToLongTimeString()

        Label1.Text = timeString

    End Sub
End Class

This post has been edited by nK0de: 16 February 2012 - 11:24 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1