|
-
Mar 10th, 2005, 03:51 PM
#1
Thread Starter
Lively Member
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..."
Last edited by milkmood; Mar 10th, 2005 at 04:12 PM.
-
Mar 10th, 2005, 04:00 PM
#2
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 10th, 2005, 04:08 PM
#3
Thread Starter
Lively Member
Re: Date and Time in Text Box
Awesome...first try...thanks a bunch. I have a new homepage.
-
Mar 10th, 2005, 04:12 PM
#4
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.
's
Ps, when you want to Resolve your thread, edit the first post so it will show at the Forum level view.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 10th, 2005, 04:13 PM
#5
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
-
Mar 10th, 2005, 04:20 PM
#6
Thread Starter
Lively Member
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
-
Mar 10th, 2005, 04:21 PM
#7
Re: Date and Time in Text Box
 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...
-
Mar 10th, 2005, 04:41 PM
#8
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 10th, 2005, 05:06 PM
#9
Re: Date and Time in Text Box
 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
-
Mar 10th, 2005, 05:36 PM
#10
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|