Results 1 to 7 of 7

Thread: [RESOLVED] Help! Display Time in a Label (VB Express 2005)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    11

    Resolved [RESOLVED] Help! Display Time in a Label (VB Express 2005)

    Hello, newbie questions here.

    I'm using VB 2005 Express.

    I want to return the current time in Label2.

    Current code:

    VB Code:
    1. Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
    2.  
    3. Label2.Text = Now.ToString("HH:MM")

    The problem is I have to click the label at run-time on the form in order for the time to show.

    How can set things up so the Time displays automatically when the form loads without me having to click the label first?


    Also, how can I display GMT or Universal Time instead of system time?

    Thanks for helping the newbie!

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Help! Display Time in a Label (VB Express 2005)

    You posted this question to the wrong forum your should have posted this to the VB.Net forum. MOD Please move this post.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Help! Display Time in a Label (VB Express 2005)

    Moved to VB.NET

  4. #4
    Lively Member
    Join Date
    Jan 2006
    Posts
    108

    Re: Help! Display Time in a Label (VB Express 2005)

    You have to click it because you have your code in the labels click event, put it in the forms load event instead.....like this
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2. Label2.Text = Now.ToString("HH:MM")
    3. End Sub
    VB.Net 2005 Express
    .Net Framework 2.0

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,451

    Re: Help! Display Time in a Label (VB Express 2005)

    Generally you would add a Timer to your form and set its Interval to 1000 (milliseconds). You would then handle the Tick event of the Timer to update your Label so it always shows the current time:
    VB Code:
    1. '24 hour time, no seconds.
    2.         Me.Label1.Text = Date.Now.ToString("HH:mm") 'Note that the "mm" is lower case because upper case means month.
    3.  
    4.         'Standard time format.
    5.         Me.Label1.Text = Date.Now.ToShortTimeString()
    6.  
    7.         'GMT.
    8.         Me.Label1.Text = Date.Now.ToUniversalTime().ToShortTimeString()

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    11

    Re: Help! Display Time in a Label (VB Express 2005)

    Thank you! She works, even got the timer in too so it updates the time.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,451

    Re: Help! Display Time in a Label (VB Express 2005)

    Cool. Don't forget to resolve your thread from the Thread Tools menu.

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