[RESOLVED] Problem with Date and Time module
Hi, i've got a module for my program thats displays the active time and date, on a form. For some reason my code is incorrect and nothing happens when I run the form. I can't figure out what the problem is, can anyone help?
Heres the code I have in the module:
Code:
Module ModDateAndTime
'Declares tmrdat as a timer with events
Public WithEvents tmrdat As Timer
'declares lbldat as a label
Public lbldat As Label
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrdat.Tick
'puts the Time and date into the label
lbldat.Text = DateAndTime.Now.ToString
End Sub
End Module
And the code in the form's:
Code:
'declares lbldat as lbldateandtime
lbldat = lblDateandTime
The relevant label in the form is named lblDateandTime
Thanks
Re: Problem with Date and Time module
Replace this:
lbldat.Text = DateAndTime.Now.ToString
with this:
lbldat.Text = Now.ToString
Re: Problem with Date and Time module
Why would you put that functionality in a module? That is straight-up presentation so it belongs in the form itself. You should add the Timer to your form and then in the Tick event handler use:
Code:
Me.lblDateandTime.Text = Date.Now.ToString()
You should use the Date data type rather than the DateAndTime module.
Re: Problem with Date and Time module
Okay i've spent a rather frustrating 15 minutes finding out that neither method works.
Both have no errors (when tried separately) yet neither shows the date and time, in the label at all.
Any suggestions?
Re: Problem with Date and Time module
Quote:
Originally Posted by
Pantero
Okay i've spent a rather frustrating 15 minutes finding out that neither method works.
Both have no errors (when tried separately) yet neither shows the date and time, in the label at all.
Any suggestions?
Both methods work perfectly. Either you're not executing the code or your referring to the wrong Label.
Re: Problem with Date and Time module
The code that I don't see: Where the timer is create, where its interval is set, and where the timer is started.
-tg
Re: Problem with Date and Time module
I've just tried both methods again, on a completely new project and still neither works, I must be doing something wrong. I don't know what, but the label doesn't change into the 'active' date and time, and i honestly don't know the reason.
Re: Problem with Date and Time module
Did you look at techgnome's comment? Unless you start your timer it will never fire the tick event and your code to set the label will never run.
Re: Problem with Date and Time module
Ah yes, I must have missed techgnome's comment. Quick question: Where would I start the timer, in the module?, each form's load event?, or somewhere else?
Re: Problem with Date and Time module
Depends... when do you want it to start?
-tg
Re: Problem with Date and Time module
When the form starts.. So I guess in the form_load procedure..?
Re: Problem with Date and Time module
Quote:
Originally Posted by
Pantero
When the form starts.. So I guess in the form_load procedure..?
That would be my guess also.:wave:
Re: Problem with Date and Time module
Ah, I already had some code in the Timer_Tick event, already and all i needed to do, was to just enable the timer.
Thanks very much for your help.