We help IT Professionals succeed at work.

vb.net 2005 timer problem

247 Views
Last Modified: 2013-11-26
I have the following code for the timer. It comiles but does not give any results!!Can someone has any idea what to do?
Public Class Form2
 
    Private WithEvents myTimer As New Timer
 
    Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick
 
        TextBox1.Text = DateTime.Now.ToString("HH:MM:ss")
 
    End Sub
End Class

Open in new window

Comment
Watch Question

Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
Hi mahmood_786,

Have you stated the timer using

myTimer.Start() ?

and also have you defined the interval ?

Author

Commented:
nope ...sorry I am beginner give some idea!!
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
On the form load you need to start the timer. You must also define the interval in miliseconds.

Something like:

    Private WithEvents myTimer As New Timer
 
    Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick
        TextBox1.Text = DateTime.Now.ToString("HH:MM:ss")
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = DateTime.Now.ToString("HH:MM:ss")
        myTimer.Interval = 1000
        myTimer.Start()
    End Sub

Open in new window

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
>> Your format is wrong..."MM" is actually for the current dates Month.  You want "mm" for minutes

Didn't notice that but he should fill the textbox in the form load or else it will see a empty textbox for a second.

Author

Commented:
But why in the following code there is no such thing!!!
Public Class Form1
    Dim str1 As Double
    Dim str2 As Double
    Dim str As String
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text += Button1.Text
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text += Button2.Text
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox1.Text += Button3.Text
    End Sub
 
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TextBox1.Text += Button6.Text
    End Sub
 
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TextBox1.Text += Button5.Text
    End Sub
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Text += Button4.Text
    End Sub
 
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        TextBox1.Text += Button7.Text
    End Sub
 
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        TextBox1.Text += Button8.Text
    End Sub
 
    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        TextBox1.Text += Button9.Text
    End Sub
 
    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        TextBox1.Text += Button15.Text
    End Sub
 
    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        TextBox1.Text += Button16.Text
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TextBox1.Enabled = False
        'TextBox1.MaxLength = 20
    End Sub
 
    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
        TextBox1.Text += Button17.Text
    End Sub
 
    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
        TextBox1.Text = ""
        str = ""
    End Sub
    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        'str = Val(TextBox1.Text)
        str2 = Val(TextBox1.Text)
        If str = "-" Then
            TextBox1.Text = str1 - str2
        ElseIf str = "+" Then
            TextBox1.Text = str1 + str2
        ElseIf str = "*" Then
            TextBox1.Text = str1 * str2
        ElseIf str = "/" Then
            TextBox1.Text = str1 / str2
        End If
 
    End Sub
 
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        str1 = Val(TextBox1.Text)
        TextBox1.Text = ""
        str = Button10.Text
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = TimeOfDay
    End Sub
 
    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        str1 = Val(TextBox1.Text)
        TextBox1.Text = ""
        str = Button11.Text
    End Sub
 
    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        str1 = Val(TextBox1.Text)
        TextBox1.Text = ""
        str = Button12.Text
    End Sub
 
    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        str1 = Val(TextBox1.Text)
        TextBox1.Text = ""
        str = Button13.Text
    End Sub
End Class

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Try this please...it is a much shorter version of your code:
Public Class Form1
 
    Dim str1 As Double
    Dim str2 As Double
    Dim str As String
 
    Private WithEvents myTimer As New Timer
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TextBox1.Enabled = False
        'TextBox1.MaxLength = 20
        myTimer.Interval = 1000
        myTimer.Start()
    End Sub
 
    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, _
            Button7.Click, Button8.Click, Button9.Click, Button15.Click, Button16.Click, Button17.Click
        TextBox1.AppendText(sender.Text)
    End Sub
 
    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles Button10.Click, Button11.Click, Button12.Click, Button13.Click
        str1 = Val(TextBox1.Text)
        TextBox1.Text = ""
        str = sender.Text
    End Sub
 
    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
        TextBox1.Text = ""
        str = ""
    End Sub
 
    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        'str = Val(TextBox1.Text)
        str2 = Val(TextBox1.Text)
        Select Case str
            Case "-"
                TextBox1.Text = str1 - str2
 
            Case "+"
                TextBox1.Text = str1 + str2
 
            Case "*"
                TextBox1.Text = str1 * str2
 
            Case "/"
                TextBox1.Text = str1 / str2
 
        End Select
    End Sub
 
    Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick
        Label1.Text = DateTime.Now.ToString("HH:mm:ss")
    End Sub
 
End Class

Open in new window

Author

Commented:
Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick
        Label1.Text = DateTime.Now.ToString("HH:mm:ss")
    End Sub


If I just right that code above to design timer it does not work!!but when in above code given before, it works!!! why?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
If you have a DESIGN-TIME Timer (placed on your form in the IDE) then you need:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = DateTime.Now.ToString("HH:mm:ss")
    End Sub

...AND... make sure in the Properties Panel, to change the Interval to 1000, and the Enabled Property to True for Timer1.

Author

Commented:
So what is DESIGN-TIME then? I love learning here!!
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Design-time is placed on the Form in the IDE by dragging and dropping.

Run-time is created via CODE while the app is running.
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
Also you don't need this:

Private WithEvents myTimer As New Timer

When you add a timer to the form (dragging from the toolbox) it will create this for you in the code behind

Author

Commented:
which file will have code behind?
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.