|
-
Apr 1st, 2003, 02:58 AM
#1
Thread Starter
Member
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.
-
Apr 1st, 2003, 06:01 AM
#2
Fanatic Member
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
-
Apr 1st, 2003, 06:01 AM
#3
Fanatic Member
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
-
Apr 1st, 2003, 07:49 AM
#4
Sleep mode
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:
Dim WithEvents TimerEvent As Timer
Dim timr As New Timer()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
timr.Interval = 2000
timr.Enabled = True
End Sub
Private Sub FireTimer(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEvent.Tick
MsgBox("Hey there")
End Sub
Private Sub StopTimer()
timr.Enabled = False
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.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
-
Apr 1st, 2003, 07:03 PM
#5
Thread Starter
Member
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.
-
Apr 2nd, 2003, 01:03 AM
#6
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|