1

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?

Alexander Mander
  • 119
  • 3
  • 13
  • Timers are a component not a control. How does `Dim t1 As New System.Windows.Forms.Timer` not do what you need? – Ňɏssa Pøngjǣrdenlarp Mar 23 '16 at 14:44
  • @Plutonix No name property unless you go to the designer window. I "think" that's what the issue is about. – LarsTech Mar 23 '16 at 14:45
  • Be careful with creating dynamic timers. The garbage collector won't know about them when the form closes, so make sure to dispose of them yourself. – LarsTech Mar 23 '16 at 14:46
  • Right, but as long as you have 3 unique references, what does the name matter? @LarsTech – Ňɏssa Pøngjǣrdenlarp Mar 23 '16 at 14:47
  • @Plutonix It doesn't, but I think the OP is flabbergasted at the inability to use the Name property. My guess. – LarsTech Mar 23 '16 at 14:48
  • Ahhh! Unlike a control where you might want to fish it out of the controls collection by name later, a component name is less interesting than the reference. Somewhat true too of controls...`btnA` is more useful than the name – Ňɏssa Pøngjǣrdenlarp Mar 23 '16 at 14:49
  • `Be careful with creating dynamic timers.` the same is true of dynamic controls: anything you create needs to be disposed manually – Ňɏssa Pøngjǣrdenlarp Mar 23 '16 at 15:04
  • @Plutonix `the same is true of dynamic controls` Not necessarily. If the controls are added to the form's control collection, they will be disposed. Timers, as components, aren't a part of any collection by default. – LarsTech Mar 23 '16 at 15:29
  • I thought so too until I encountered a leak on a app that had to create many per minute. My encounter was pre-SO, so I had to figure it out for myself, but Hans has numerous posts here warning of it..[for example](http://stackoverflow.com/a/2014427). I added a "reporter" to the dispose method of a control to work out that they werent disposed. he also has the famous "parking lot" post somewhere. @LarsTech – Ňɏssa Pøngjǣrdenlarp Mar 23 '16 at 15:34
  • Guys, the question is how do I create three timers and give them each a different "name" so I can disable a few of them later? Like, I want to create a loop (3 times loop), and everytime it should create one timer (so 3 timers in general). I want to reference then the first and second timer later and disable only them (without touching the third timer). How do I accomplish that? – Alexander Mander Mar 24 '16 at 01:17
  • The answer is clear... you don't create them dynamically. You define them all. You then use other controls/events to start whichever ones need starting and the same to stop them. So you could have them start/stop when a form opens or a page loads or a button is clicked etc. – Mych Mar 24 '16 at 12:34

2 Answers2

1

'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

Jande
  • 1,695
  • 2
  • 20
  • 32
  • Guys, the question is how do I create three timers and give them each a different "name" so I can disable a few of them later? Like, I want to create a loop (3 times loop), and everytime it should create one timer (so 3 timers in general). I want to reference then the first and second timer later and disable only them (without touching the third timer). How do I accomplish that? – Alexander Mander Mar 24 '16 at 01:17
  • 1
    @SebastianAhlen i changed my answer. Is this what you want – Jande Mar 24 '16 at 23:01
0

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
Mech_Engineer
  • 535
  • 1
  • 19
  • 46