Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

gTimePicker- Control to Pick a Time Value (VB.NET)

, 8 Feb 2012 CPOL
Stop using the DateTimePicker for time values. This control makes it easy to pick a time, and if you act now, get the matching Nullable gDateTimePicker at no extra cost.
Public Class Form1

    Private Sub GTimePicker2_TimePicked(ByVal sender As System.Object) Handles GTimePicker2.TimePicked
        TextBox1.Text = GTimePicker2.ToStringAMPM
    End Sub

    Private Sub GTimePicker1_TimePicked(ByVal sender As System.Object) Handles GTimePicker1.TimePicked
        Label1.Text = "You picked " & GTimePicker1.Time
    End Sub

    Private Sub GDateTimePicker2_ValueOrNullChanged(ByVal sender As System.Object) Handles GDateTimePicker2.ValueOrNullChanged
        UpdateDateTime()
    End Sub

    Sub UpdateDateTime()
        TextBox2.Text = String.Concat(GDateTimePicker2.ValueToString(DateTimePickerFormat.Long), " ", GTimePicker4.ToStringAMPM)
    End Sub

    Private Sub GTimePicker4_TimePicked(ByVal sender As System.Object) Handles GTimePicker4.TimePicked
        UpdateDateTime()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GDateTimePicker1.gValue = Nothing
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        GDateTimePicker1.gValue = Today
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        GDateTimePicker1.gFormat = DateTimePickerFormat.Short
    End Sub

    Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
        GDateTimePicker1.gFormat = DateTimePickerFormat.Long
    End Sub

    Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
        GDateTimePicker1.gFormat = DateTimePickerFormat.Custom
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        GDateTimePicker1.gFormatString = TextBox4.Text
    End Sub

    Private Sub GDateTimePicker1_ValueOrNullChanged(ByVal sender As Object) Handles GDateTimePicker1.ValueOrNullChanged
        TextBox3.Text = GDateTimePicker1.ValueToString(OutputString)
    End Sub

    Dim OutputString As DateTimePickerFormat = DateTimePickerFormat.Long

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        OutputString = DateTimePickerFormat.Short
        TextBox3.Text = GDateTimePicker1.ValueToString(OutputString)
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        OutputString = DateTimePickerFormat.Long
        TextBox3.Text = GDateTimePicker1.ValueToString(OutputString)
    End Sub

    Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton6.CheckedChanged
        OutputString = DateTimePickerFormat.Custom
        TextBox3.Text = GDateTimePicker1.ValueToString(OutputString)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form2.ShowDialog()

    End Sub

    Private tTime As String
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblNowTime.Text = Now.ToLongTimeString

        If CheckBox1.Checked Then
            Dim nTime As String = String.Format("{0}:{1} {2}", _
                                      Now.Hour, _
                                      Now.Minute, _
                                      IIf(Now.Hour < 12, "AM", "PM"))
            If GTimePickerCntrl1.Time <> String.Empty _
              AndAlso tTime <> nTime Then
                GTimePickerCntrl1.Time = Now.ToShortTimeString
                tTime = nTime
            End If
        End If
    End Sub

    Private Sub GTimePickerCntrl1_TimePicked(ByVal sender As System.Object) Handles GTimePickerCntrl1.TimePicked
        tTime = String.Format("{0:0#}:{1:0#} {2}", _
                    GTimePickerCntrl1.Hour, _
                    GTimePickerCntrl1.Minute, _
                    GTimePickerCntrl1.TimeAMPM.ToString)
        Label3.Text = GTimePickerCntrl1.Time
        Label4.Text = tTime
        Label5.Text = GTimePickerCntrl1.ToStringAMPM
        Label6.Text = GTimePickerCntrl1.TimeAMPM.ToString
        Label7.Text = GTimePickerCntrl1.Hour
    End Sub

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Timer1.Start()
    End Sub

    Private Sub chkMidMins_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMidMins.CheckedChanged
        GTimePickerCntrl1.ShowMidMins = chkMidMins.Checked
    End Sub

    Private Sub chkTrueHour_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTrueHour.CheckedChanged
        GTimePickerCntrl1.TrueHour = chkTrueHour.Checked
    End Sub

    Private Sub chk24Hour_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk24Hour.CheckedChanged
        GTimePickerCntrl1.Hr24 = chk24Hour.Checked

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        GTimePickerCntrl1.Time = TextBox5.Text
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SSDiver2112
Software Developer
United States United States
I first got hooked on programing with the TI994A. After it finally lost all support I reluctantly moved to the Apple IIe. Thank You BeagleBros for getting me through. I wrote programs for my Scuba buisness during this time. Currently I am a Database manager and software developer. I started with VBA and VB6 and now having fun with VB.NET

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 8 Feb 2012
Article Copyright 2009 by SSDiver2112
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid