Date and Time in Text Box
N00b here...
I need to put current date and time in a text box. I'm told I need to use a timer, so what's the code going to look like for the timer_tick, and the text box_???
I know it's dead simple, but like I said...n00B. But isn't that what forums are for?
Thanks,
CP
"...my brain is full..." :eek2:
Re: Date and Time in Text Box
Welcome to the Forums.
Its not really much different then in VB6.
Drop a timer on to your form from the toolbox. Then add these procedures to your forms code, assuming that
you already have a textbox on your form.
VB Code:
Private CurrentTime As DateTime = DateTime.Now
[color=darkgray]"Windows Form Designer generated code"[/color]
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.tmrTimer.Interval = 1000
Me.tmrTimer.Enabled = True
End Sub
Private Sub tmrTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
Static dtmPrevious As DateTime
'Store away the current time. If the second has changed, store away the new current
'time in dtmPrevious, and then invalidate the parent form.
CurrentTime = DateTime.Now
If CurrentTime.Second <> dtmPrevious.Second Then
dtmPrevious = CurrentTime
Me.TextBox1.Text = CurrentTime
End If
End Sub
:thumb:
Re: Date and Time in Text Box
Awesome...first try...thanks a bunch. I have a new homepage.
:thumb:
Re: Date and Time in Text Box
No prob. Glad to help.
I think you will find that we have many members that are top notch and very willing to help.
:thumb:'s
Ps, when you want to Resolve your thread, edit the first post so it will show at the Forum level view. ;)
Re: Date and Time in Text Box
rob, im curious as to why the extra code over something like this?
VB Code:
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.tmrTimer.Interval = 1000
Me.tmrTimer.Enabled = True
End Sub
Private Sub tmrTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
Me.TextBox1.Text = DateTime.Now.ToString
End Sub
Re: Date and Time in Text Box
That works, too...does exactly the same thing with half the bytes.
Hope I didn't open a can o'worms over something so simple.
CP
Re: Date and Time in Text Box
Quote:
Originally Posted by milkmood
That works, too...does exactly the same thing with half the bytes.
CP
set the timer properties at design time and you can remove the code from the form load event too...
Re: Date and Time in Text Box
The extra code in the form load event is by my personal habit of explicitly setting properties.
No harm in removing it.
Just personal preference I suppose.
Re: Date and Time in Text Box
Quote:
Originally Posted by RobDog888
The extra code in the form load event is by my personal habit of explicitly setting properties.
No harm in removing it.
Just personal preference I suppose.
no i meant the second checking code in the timer event
Re: Date and Time in Text Box
Oh, that to make sure the second's display is more accurate and depends on the interval setting also.