Link to home
Start Free TrialLog in
Avatar of mahmood_786
mahmood_786Flag for United Kingdom of Great Britain and Northern Ireland

asked on

vb.net 2005 timer problem

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

Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Hi mahmood_786,

Have you stated the timer using

myTimer.Start() ?

and also have you defined the interval ?
Avatar of mahmood_786

ASKER

nope ...sorry I am beginner give some idea!!
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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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.
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

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

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?
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.
So what is DESIGN-TIME then? I love learning here!!
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.
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
which file will have code behind?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial