[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:
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
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!
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.
Re: Help! Display Time in a Label (VB Express 2005)
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label2.Text = Now.ToString("HH:MM")
End Sub
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:
'24 hour time, no seconds.
Me.Label1.Text = Date.Now.ToString("HH:mm") 'Note that the "mm" is lower case because upper case means month.
'Standard time format.
Me.Label1.Text = Date.Now.ToShortTimeString()
'GMT.
Me.Label1.Text = Date.Now.ToUniversalTime().ToShortTimeString()
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.
Re: Help! Display Time in a Label (VB Express 2005)
Cool. Don't forget to resolve your thread from the Thread Tools menu.