Create timers dynamically and add unique names VB.net - vb.net

Alright,
I know how to create buttons and give them each unique names to access them
I do it like this
Dim btnName As String
Dim x As Short
For i As Short = 1 To 3
btnName = "button" & CStr(i)
x += 3
Dim button1 As New Button
button1.Name = btnName
Me.Controls.Add(button1)
button1.Location = New Point(10, x * 10)
button1.Text = "Hello" & i
Next
When I try to create a timer, I cannot give it a name like I did above with the buttons
btnName = "button" & CStr(i)
button1.Name = btnName
So I don't know how to access them and/or activate them for instance. I want to create like three timers and name them like "timer1", "timer2", "timer3"
How do I achieve that?

'Here Is a form code that starts a timer on a button click
Public Class Form1
Dim t1 As Timer
Dim t2 As Timer
Dim t3 As Timer
Private Sub btnT1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT1.Click
'on btn click start timer1
t1 = New Timer
t1.Tag = DateTime.Now
AddHandler t1.Tick, AddressOf MyTickHandler
t1.Start()
End Sub
Private Sub btnT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT2.Click
'on btn click start timer2
t2 = New Timer
t2.Tag = DateTime.Now
AddHandler t2.Tick, AddressOf MyTickHandler
t2.Start()
End Sub
Private Sub btnT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT3.Click
'on btn click start timer3
t3 = New Timer
t3.Tag = DateTime.Now
AddHandler t3.Tick, AddressOf MyTickHandler
t3.Start()
End Sub
Sub MyTickHandler(ByVal sender As Object, ByVal e As EventArgs)
dim t As Timer = DirectCast(sender, Timer)
dim timerString = "The timer started at " & t.Tag.ToString & " just ticked..."
End Sub
Private Sub btnStopT1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopT1.Click
'stop timer1
t1.Stop()
t1.Dispose()
End Sub
Private Sub btnStopT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopT2.Click
'stop timer 2
t2.Stop()
t2.Dispose()
End Sub
Private Sub btnStopT3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopT3.Click
'stop timer 3
t3.Stop()
t3.Dispose()
End Sub
End Class
Timer Class

This tutorial should help you create a timer object.
Tutorial 2: Create a Timed Math Quiz
Tutorial link
In detail step 3 will do the trick.
Step 3: Add a Countdown Timer
Link to step 3
This piece of shows how you can do something with the tick event (copy from the MSDN site)
Private Sub Timer1_Tick() Handles Timer1.Tick
If timeLeft > 0 Then
' Display the new time left
' by updating the Time Left label.
timeLeft -= 1
timeLabel.Text = timeLeft & " seconds"
Else
' If the user ran out of time, stop the timer, show
' a MessageBox, and fill in the answers.
Timer1.Stop()
timeLabel.Text = "Time's up!"
MessageBox.Show("You didn't finish in time.", "Sorry!")
sum.Value = addend1 + addend2
startButton.Enabled = True
End If

Related

'stop' & 'start' is not a member of System.Windows.Forms.Timer vb.net

I am trying to start and stop a timer and it keeps telling me that they are both not a member of System.Windows.Forms.Timer
Below is my code. I used this in a different windows project and it works fine, don't know if I have to initialize it differently in this project.
Private Sub InitializeComponent()
Me.timer = New System.Windows.Forms.Timer
End Sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles timer.Tick
counter -= 1
If counter = 0 Then
timer.Stop()
lblPrice.Text = "$0.00"
lblBarcode.Text = "Place Item Near Scanner"
lblName.Text = ""
lblID.Text = ""
lblUnit.Text = "Price Checker"
counter = 8
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
timer = New System.Windows.Forms.Timer
AddHandler timer.Tick, AddressOf Me.timer_Tick
timer.Interval = 1000
timer.Start()
End Sub

Progress Bar increments at twice the intended speed each time

Private Time As New Timer
Private Sub btnWood_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWood.Click
prgWood.Value = 0
Time.Interval = 1000
Time.Start()
AddHandler Time.Tick, AddressOf IncreaseProgressBar
If prgWood.Value <> prgWood.Maximum Then
btnWood.Enabled = False
End If
Dim intAmountofWood As Integer = 11 * Rnd() + 10
intWood = intWood + intAmountofWood
Me.lblWoodAmount.Text = intWood
Private Sub IncreaseProgressBar(ByVal sender As Object, ByVal e As EventArgs)
prgWood.Increment(10)
If prgWood.Value = prgWood.Maximum Then
prgWood.Increment(0)
Time.Stop()
btnWood.Enabled = True
End If
End Sub
For my progress bar, I use a Timer to increment the value by 10 every 1 second. When I debug the project, it works fine the first time (taking 10 seconds for the progress bar to complete) but when I click the button a second time, it only takes 5 seconds, then less and less each time. This code is for an incremental game I'm trying to make for school.
From LarsTech's comment:
Public Class Form1
Private Time As New Timer
Public Sub New()
'Initialisation, etc
AddHandler Time.Tick, AddressOf IncreaseProgressBar
End Sub
'Other methods, etc
End Class
Then you need to remove the AddHandler from the button
click event
Can you do this?
Create a sub and add this code
`
ProgressBar1.Value = e.ProgressPercentage
If ProgressBar1.Value = ProgressBar1.Maximum Then
ProgressBar1.Value = ProgressBar1.Minimum
End If
`
then call that sub here.
`Private Sub btnWood_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWood.Click
'**Name of the Sub**
prgWood.Value = 0
Time.Interval = 1000
Time.Start()
AddHandler Time.Tick, AddressOf IncreaseProgressBar
If prgWood.Value <> prgWood.Maximum Then
btnWood.Enabled = False
End If
Dim intAmountofWood As Integer = 11 * Rnd() + 10
intWood = intWood + intAmountofWood
Me.lblWoodAmount.Text = intWood
End Sub
`
let see if that one works

read line by line from richtextbox and show in label (vb.net)

I would like to read line by line from richtextbox and show each line every a second in label.
I have this code blocks.
and I think I need a timer but I couldnt make it.
can you help me ?
Remarks :
If I use this code , I can only see the last line in label.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RotateCount As String()
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Label1.Text = RichTextBox1.Lines(i)
Next
End Sub
I mean, assume that we have lines in richtextbox like..
a1
b2
c3
d4
e5
and I would like to show label1 in for each second like..
a1
(after 1 sec.)
b2
(after 1 sec.)
c3
(after 1 sec.)
like this...
You seems to expect that, because you set the Text property, the label repaints itself immediately with the new text. This doesn't happen until you exit from the event handler and the system could repaint the label. Of course, with this code, only the last text is shown.
To reach your goal, you could use a Timer set to 1 second interval and a counter that keeps track of the current line dispayed:
Dim tm As System.Windows.Forms.Timer = new System.Windows.Forms.Timer()
Dim counter As Integer = 0
At this point your button click just start the timer and exits
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tm.Interval = 1000
AddHandler tm.Tick, AddressOf onTick
tm.Start()
' Don't allow to click again this button until
' the timer is stopped
Button1.Enabled = False
Button2.Enabled = True
End Sub
When the Tick event is raised you change the label text to the line indexed by the counter, increment it and check if you have reached the last line restarting from the first one if this is the case. Note that the button is disabled before exiting. This is required to avoid a second/third/fourth/etc click on the same button while the timer runs..... More on Button2 later....
Sub onTick(sender as Object, e as EventArgs)
Label1.Text = RichTextBox1.Lines(counter)
counter += 1
if counter >= RichTextBox1.Lines.Count Then
counter = 0
End If
End Sub
Of course, now you need another button to stop the Timer run and reenable the first button
' This button stops the timer and reenable the first button disabling
' itself - It should start as disabled from the form-designer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
tm.Stop
RemoveHandler tm.Tick, AddressOf onTick
Button1.Enabled = True
Button2.Enabled = False
End Sub
You're almost there. Your problem is that you keep setting the text, not adding to it. Label1.Text = ... sets the text, if you want to keep adding to it you'd use Label1.Text &= ...
Also note that you need to include something like Environment.NewLine in order to include line breaks.
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Label1.Text &= RichTextBox1.Lines(i) & If(i < RichTextBox1.Lines.Length - 1, Environment.NewLine, "")
Next
thank you for your help !!!
I solved with this code ;
Public Class Form1
Dim tm = New System.Windows.Forms.Timer()
Dim counter As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Sub onTick(sender As Object, e As EventArgs)
Label1.Text = RichTextBox1.Lines(counter)
counter += 1
If counter >= RichTextBox1.Lines.Count Then
counter = 0
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For i = 0 To RichTextBox2.Lines.Count - 1
TextBox1.Text = RichTextBox2.Lines(i)
wait(2000)
Next
End Sub
Private Sub wait(ByVal interval As Integer)
Dim sw As New Stopwatch
sw.Start()
Do While sw.ElapsedMilliseconds < interval
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
End Class
It is very simple.
Declare one more string variable and load all string to this variable .
Improved code is given below.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As system.EventArgs) Handles Button1.Click
Dim a1 as int32
a1=0
'get number of lines of the rich text box content
a1=RichTextBox1.Lines.Count()
Dim str As String
For i As Int32 = 0 To a1-1
str = str + RichTextBox1.Lines(i)
Label1.Text= str
Next
End Sub

How to handle a variable amount of buttons clicked?

I have a pretty basic problem with button clicks in VB.Net I cannot seem to figure out.
First, I am creating a variable amount of buttons and adding them to the parent form.
Private Sub CreateUIObjects()
For i As Integer = 1 To NumberOfButtons
Dim button As Button = New Button()
Me.Controls.Add(button)
Next
End Sub
I know it is possible to handle a fixed amount of buttons clicked with the following code
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click, Button2.Click, Button3.Click '... And so on
Dim b As Button = CType(sender, Button)
End Sub
But what do I do with not 3, but a variable amount of buttons?
Some code to experiment with
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CreateUIObjects(3)
End Sub
Dim myButtonNames As String = "foobar"
Private Sub myButtonsClick(sender As Object, e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
Debug.WriteLine(b.Name)
End Sub
Private Sub CreateUIObjects(NumberOfButtons As Integer)
Static ct As Integer = 0
For i As Integer = 1 To NumberOfButtons
ct += 1
Dim btn As Button = New Button()
btn.Name = myButtonNames & ct.ToString
btn.Text = btn.Name
btn.Location = New Point(ct * 20, ct * 20)
AddHandler btn.Click, AddressOf myButtonsClick
Me.Controls.Add(btn)
Next
End Sub
You can use something like this
AddHandler Button1.Click, AddressOf Button_Click
take a look http://msdn.microsoft.com/en-us/library/ms172877.aspx

how to create a picturebox in vb 2008 and move it?

I just want to ask a really important question in vb2008 :
I'm working on 2D level designer that I just put all the images and the collision rectangles in his place then the program generate the right code.
the problem is : I make a button and in his click event it add a new picture box , when it add I have to move it with the mouse , I have the "move" code but like you know it's a new picture box so I can't write the code before the picture box add.(if you doesn't understand I can explain my situation again)
for more clear this is the code :
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer
newPictureBox.Image = Image.FromFile("C:\Users\hp\Desktop\ground.bmp")
newPictureBox.Name = "image" & (i)
newPictureBox.Visible = True
newPictureBox.Top = 200
newPictureBox.Width = 100
newPictureBox.Height = 50
newPictureBox.Left = 100 + goToRight
newPictureBox.SizeMode = PictureBoxSizeMode.AutoSize
'add control to form
Controls.Add(newPictureBox)
goToRight = goToRight + newPictureBox.Width
i += 1
End Sub
and this is the "move picturebox" code(it's for an existed picturbox):
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim endPoint As Integer = Val(TextBox1.Text)
Label1.Text = "X: " & MousePosition.X - (Location.X + 8)
Label2.Text = "Y: " & MousePosition.Y - (Location.Y + 29)
RadioButton1.Left = endPoint + RadioButton2.Left
Panel1.Left = 0
'Move picture code :
If IcanMove = True Then
PictureBox1.Left = MousePosition.X - (Location.X + 8) - differenceX
PictureBox1.Top = MousePosition.Y - (Location.Y + 29) - differenceY
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
IcanMove = True
differenceX = (MousePosition.X - (Location.X + 8)) - PictureBox1.Left
differenceY = (MousePosition.Y - (Location.Y + 29)) - PictureBox1.Top
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
IcanMove = False
End Sub
thanks you for reading :)
You need to add event handlers for the newly added PictureBox, here is how to do that.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NewPictureBox As PictureBox = New PictureBox
NewPictureBox.Parent = Me
NewPictureBox.Location = New Point(100, 100)
Me.Controls.Add(NewPictureBox)
'you can use existing event handlers like your PictureBox1_MouseDown
AddHandler NewPictureBox.MouseUp, AddressOf PictureBox_MouseUp
AddHandler NewPictureBox.MouseDown, AddressOf PictureBox_MouseDown
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
MessageBox.Show("Triggered MouseDown Event")
End Sub
Private Sub PictureBox_MouseUp(sender As Object, e As MouseEventArgs)
MessageBox.Show("Triggered MouseUp Event")
End Sub
(if you also remove the PictureBox's before closing the form be sure to remove the handlers for them)
For more info check AddHandler and RemoveHandler