Results 1 to 6 of 6

Thread: Timer Created During Runtime

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2003
    Posts
    32

    Timer Created During Runtime

    How can I add procedure to Timers that are created during runtime?

    Code:
    Public Function NewApple()
            Dim picNewApple As PictureBox = New PictureBox()
            Dim tmrNewApple As Timer = New Timer()
            Dim AppleType As Integer = RandomNumber(100)
            Select Case AppleType
                Case Is >= 50
                    picNewApple.Image = Image.FromFile(Application.StartupPath & "\RedApple.gif")
                Case Is >= 25
                    picNewApple.Image = Image.FromFile(Application.StartupPath & "\GreenApple.gif")
                Case Is >= 15
                    picNewApple.Image = Image.FromFile(Application.StartupPath & "\GoldApple.gif")
                Case Is >= 0
                    picNewApple.Image = Image.FromFile(Application.StartupPath & "\WormApple.gif")
            End Select
            picNewApple.SizeMode = PictureBoxSizeMode.AutoSize
            picNewApple.Location = New System.Drawing.Point(RandomNumber(600), -51)
            picNewApple.BackColor = System.Drawing.Color.Transparent
            picNewApple.SendToBack()
            Me.Controls.Add(picNewApple)
        End Function
    I want the timer that is created to control the movement of the newly created apple. Anyone know?
    Last edited by LuvKnight; Apr 1st, 2003 at 07:10 PM.

  2. #2
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724
    A simple one for better understanding

    Code:
    ' form leve
        Dim WithEvents Tm As Timer
    
    
    'on button cick
    
                   Tm = New Timer()
                    Tm.Interval = 100
                    Tm.Enabled = True
    
    ' timer event.
        Private Sub Tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tm.Tick
            StatusBar1.Panels(0).Text = System.DateTime.Now.ToString
        End Sub

  3. #3
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724
    A simple one for better understanding

    Code:
    ' form leve
        Dim WithEvents Tm As Timer
    
    
    'on button cick
    
                   Tm = New Timer()
                    Tm.Interval = 100
                    Tm.Enabled = True
    
    ' timer event.
        Private Sub Tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tm.Tick
            lab1.Text = System.DateTime.Now.ToString
        End Sub

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Here's another way to do it that uses the power of addhandler method : Addhandler can fires one or multiple events or methods at the same time . This is also called delegates.

    VB Code:
    1. Dim WithEvents TimerEvent As Timer
    2.     Dim timr As New Timer()
    3.    
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         timr.Interval = 2000
    6.         timr.Enabled = True
    7.     End Sub
    8.  
    9.     Private Sub FireTimer(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEvent.Tick
    10.         MsgBox("Hey there")
    11.     End Sub
    12.  
    13.     Private Sub StopTimer()
    14.         timr.Enabled = False
    15.     End Sub
    16.  
    17.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    18.         AddHandler timr.Tick, AddressOf FireTimer
    19.     End Sub
    20.  
    21.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    22.         timr.Enabled = False
    23.     End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2003
    Posts
    32
    I don't really understand what is going on.

    I create a new picturebox at 1 second interval, so I need a timer to control that picturebox every 1 second. To make to do picturebox.top = picturebox.top + 1

    Anybody can do a example function, the part of the picturebox is done above.

  6. #6
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724
    Originally posted by LuvKnight
    I don't really understand what is going on.

    I create a new picturebox at 1 second interval, so I need a timer to control that picturebox every 1 second. To make to do picturebox.top = picturebox.top + 1

    Anybody can do a example function, the part of the picturebox is done above.
    taking the example from Pirate i will try to solve ur problem
    place 2 button one for start and one for stop

    Code:
    Public Class Form1
    
    
        Dim picNewApple As PictureBox = New PictureBox()
        Dim WithEvents TimerEvent As Timer
        Dim timr As New Timer()
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim img As Bitmap
            timr.Interval = 2000
            timr.Enabled = True
            Me.Controls.Add(picNewApple)
            img = New Bitmap("D:\untitled.bmp")
            picNewApple.SizeMode = PictureBoxSizeMode.AutoSize
            picNewApple.Image = img
        End Sub
    
    
    
        Private Sub FireTimer(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEvent.Tick
            picNewApple.Top += picNewApple.Top + 1
        End Sub
    
        Private Sub StopTimer()
            timr.Enabled = False
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            AddHandler timr.Tick, AddressOf FireTimer
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            timr.Enabled = False
        End Sub
    if u feel its too slow then change the timr.Interval = 200


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width