שתף באמצעות


Assign Time to a ComboBox Item

Question

Saturday, January 25, 2014 9:48 PM

Its been a few years since Ive done anything in VB. I have a ComboBox Item that I need to assign a Time to. My Items are simply 12 am, 1 am, 2 am, etc. they will be a starting point for a calculation. I need to convert these items for my calculation to a military time stamp. i.e. 12 am = 00:00:00... 1 pm = 13:00:00. HH:MM:SS. Any ideas on how I can do this? Any feedback would be appreciated.

All replies (4)

Sunday, January 26, 2014 12:12 AM ✅Answered | 1 vote

Hi,

 Here is a quick example of filling a combobox with times from 12:00AM to 11:PM and when you select a time it uses Date.TryParse to convert the selected items text into a Date variable and then shows it on the form with a label as 00:00:00 to 23:00:00 hours.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

        'Fills the combobox with times 1 hour appart from 12:00AM to 11:00PM
        For x = 0 To 23
            Dim tm As New Date(1, 1, 1, x, 0, 0)
            ComboBox1.Items.Add(tm.ToShortTimeString)
        Next
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        'Get the selected time into a Date vaiable Date.TryParse and display it as a military time format string
        Dim dt As Date
        If Date.TryParse(ComboBox1.SelectedItem.ToString, dt) Then
            Label1.Text = dt.ToString("HH:mm:ss")
        End If
    End Sub
End Class

@ Frank - Nice Class. Looks handy for dealing with time conversions.   :)


Saturday, January 25, 2014 10:16 PM | 1 vote

Its been a few years since Ive done anything in VB. I have a ComboBox Item that I need to assign a Time to. My Items are simply 12 am, 1 am, 2 am, etc. they will be a starting point for a calculation. I need to convert these items for my calculation to a military time stamp. i.e. 12 am = 00:00:00... 1 pm = 13:00:00. HH:MM:SS. Any ideas on how I can do this? Any feedback would be appreciated.

Most likely you'll want to hand it strings. If you're starting with a DateTime object, you can format the DateTime object using various formats as shown here.

I meant to add this:

You might want to make your work easier by using a pre-formatted one by using the ToShortTimeString method.


Saturday, January 25, 2014 11:25 PM | 1 vote

Or ... let's do our own ;-)

Public Class TimeObject
    Private _dt As DateTime

    Public Sub New(ByVal dt As DateTime)

        _dt = dt

    End Sub

    Public ReadOnly Property InstanceDateTime() As DateTime
        Get
            Return _dt
        End Get
    End Property

    Public ReadOnly Property ShortTimeFormat_12HourMinute() As String
        Get
            Return _dt.ToString("hh:mm")
        End Get
    End Property

    Public ReadOnly Property LongTimeFormat_12HourMinute() As String
        Get
            Return _dt.ToString("hh:mm tt")
        End Get
    End Property

    Public ReadOnly Property ShortTimeFormat_12HourMinuteSeconds() As String
        Get
            Return _dt.ToString("hh:mm:ss")
        End Get
    End Property

    Public ReadOnly Property LongTimeFormat_12HourMinuteSeconds() As String
        Get
            Return _dt.ToString("hh:mm:ss tt")
        End Get
    End Property

    Public ReadOnly Property ShortMilitaryTimeFormat() As String
        Get
            Return _dt.ToString("HH:mm")
        End Get
    End Property

    Public ReadOnly Property LongMilitaryTimeFormat() As String
        Get
            Return _dt.ToString("HH:mm:ss")
        End Get
    End Property

    Public Function ToShortTimeFormat_12HourMinute() As String
        Return ShortTimeFormat_12HourMinute
    End Function

    Public Function ToLongTimeFormat_12HourMinute() As String
        Return LongTimeFormat_12HourMinute
    End Function

    Public Function ToShortTimeFormat_12HourMinuteSeconds() As String
        Return ShortTimeFormat_12HourMinuteSeconds
    End Function

    Public Function ToLongTimeFormat_12HourMinuteSeconds() As String
        Return LongTimeFormat_12HourMinuteSeconds
    End Function

    Public Function ToShortMilitaryTimeFormat() As String
        Return ShortMilitaryTimeFormat
    End Function

    Public Function ToLongMilitaryTimeFormat() As String
        Return LongMilitaryTimeFormat
    End Function

    Public Overrides Function ToString() As String

        Dim sb As New System.Text.StringBuilder

        sb.AppendLine("Instance DateTime: " & _dt.ToString)
        sb.AppendLine()
        sb.AppendLine("Short Time Format (12-Hour, Hour:Minutes): " & ShortTimeFormat_12HourMinute)
        sb.AppendLine("Short Time Format (12-Hour, Hour:Minutes:Seconds): " & ShortTimeFormat_12HourMinuteSeconds)
        sb.AppendLine()
        sb.AppendLine("Long Time Format (12-Hour, Hour:Minutes): " & LongTimeFormat_12HourMinute)
        sb.AppendLine("Long Time Format (12-Hour, Hour:Minutes:Seconds): " & LongTimeFormat_12HourMinuteSeconds)
        sb.AppendLine()
        sb.AppendLine("Short Military Time (24-Hour, Hour:Minutes): " & ShortMilitaryTimeFormat)
        sb.AppendLine("Long Military Time (24-Hour, Hour:Minutes:Seconds): " & LongMilitaryTimeFormat)
       
        Return sb.ToString

    End Function

End Class

Something to consider. :)

Please call me Frank :)


Sunday, January 26, 2014 12:21 AM

@ Frank - Nice Class. Looks handy for dealing with time conversions.   :)

I suspect that you've answered his immediate question, but I also suspect there will be a follow-up of "oh ok thanks, now how do I ...", so I thought I'd get ahead of the curve. ;-)

Please call me Frank :)