Results 1 to 4 of 4

Thread: [RESOLVED] DateTimePicker vb.net 2005

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    24

    Resolved [RESOLVED] DateTimePicker vb.net 2005

    I've got the following piece of code that shows current date and time in a label within timer_tick.
    What i want to do is to enable a user to choose time format ie 12 or 24 hr clock.
    I know it's something to do with the DateTimePicker but i dont know how to incorporate it within my code.

    My time code is the simple code below:

    "Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Label1.Text = Now.ToString
    End Sub"

    Please help!

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

    Re: DateTimePicker vb.net 2005

    If all you want to do is select 12 or 24 hour format for a time displayed in a Label then it has nothing to do with a DateTimePicker. It would only involve a DTP if you wanted to display the time in a DTP. You should create a ComboBox and add items "12 hour" and "24 hour" to it. In your Tick event handler you would do this:
    vb Code:
    1. Dim timeFormat As String
    2.  
    3. If timeFormatCombo.SelectedItem = "24 hour" Then
    4.     timeFormat = "HH:mm:ss"
    5. Else
    6.     timeFormat = "h:mm:ss tt"
    7. End If
    8.  
    9. timeLabel.Text = Date.Now.ToString(timeFormat)
    Probably even better would be if you declared a class-level variable named timeFormat which you set on the SelectedIndexChanged event of your ComboBox:
    vb Code:
    1. If Me.timeFormatCombo.SelectedItem = "24 hour" Then
    2.     Me.timeFormat = "HH:mm:ss"
    3. Else
    4.     Me.timeFormat = "h:mm:ss tt"
    5. End If
    Then in the Tick event handler there's no need to calculate the format each time. You just go ahead and use the one that was set last time the user made a selection:
    vb Code:
    1. Me.timeLabel.Text = Date.Now.ToString(Me.timeFormat)
    Last edited by jmcilhinney; Mar 17th, 2007 at 07:02 PM.
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    24

    Re: DateTimePicker vb.net 2005

    Thanks jmcilhinney!
    Just a quick question. What do i "Dim timeFormatCombo as"? , and i've put Dim timeLabel As label, is that right?

    Thank you!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    24

    Re: DateTimePicker vb.net 2005

    Hooray!!! jmcilhinney Thank you very much.
    It's all working now.
    Once again thank you

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