Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign Timer "Name" to Variable 1

Status
Not open for further replies.

Fletch12

IS-IT--Management
Aug 13, 2002
140
US
Is there a way to assign the name property of a timer in vb.net to a variable?

I have quite a few timers on my form and need a way to assign the name of each timer to a variable.

Dim timername as String
timername = timer1.???
 
Dim timername As Timer
timername = Me.nameoftimer

You can then reference all properties....

timername.Start()
timername.Stop()

etc.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
I think I might have been a bit hasty in answering your questions becuaue looking at it again, I don't think I really did answer it.

To answer - I don't think there is any way to reference the "name" of a timer. Instead, you would want to use something like I started to show in my previous post.

You create an array of the timers on the form, and iterate through the array when necessary.

You could also probably use the Tag property and put the name there are well. That would expose the name to you, but you would have to add that to each Timer manually.

Finally, if neither of those suggestions help, please elaborate a bit so we can see what else it is you are trying to do....

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
I didn't explain my question very well...

I have a sub which handles the tick event of multiple timers, and I need to know which timer has been called, and then assign the name of that timer to a variable.

Code:
Private Sub eqptime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eqptime1.Tick,eqptime2.Tick,eqptime3.Tick,eqptime4.Tick,eqptime5.Tick
...
Dim timername as String
timername = <which timer was called>.???
End Sub
 
Okay...how do I retrieve the name of the sender though?
 
That actually should make things a bit easier if you are using some part of the Timer class, such as Start or Stop. The first parameter in your sub is 'sender'. This already identifies to the sub which control is calling the code. So you should be able to use something like:

sender.Start() or sender.Stop()

within in the sub.

If you are going to use this variable in other places, then I think your best bet is one of the following:

1. Use the Tag property as I stated. Put the name of the Timer in the Tag property and then you can use timername = sender.Tag

2. Use a Case statement to evaluate which Timer is calling the code and assign the variable -

Select Case sender
Case Timer1
timername = "some value"
Case Timer2
timername = "another value"
End Select

Either method should produce the same results. The Tag method provides a bit more flexibility with less code, but the Case method is sometimes easier to read and understand when you have to return to the code at a later time.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
The ".tag" property does not seem to be available for the timer control.
 
If you have Option Strict On, you will need this

DirectCast(sender, Timer).Tag.ToString

instead of just sender to convert the sender to a Timer and expose the properties.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
When I attempt this I get...

'Tag' is not a member of 'System.Windows.Forms.Timer'
 
Can you post what you code you have??? I tested that last before posting and it works. I suspect maybe you have something incorrect in your code.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Apologies, because [tt](Name)[/tt] is in the PropertyGrid for a Timer (like all other controls), I assumed that it did have a Name property.

Although I don't like the Tag property, unless you create your own inherited Timer Control and give it a Name property, that does look like easiest solution.

[vampire][bat]
 
Here is the code I have...

Code:
    Private Sub eqptime1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eqptime1.Tick, eqptime2.Tick, _
    eqptime3.Tick, eqptime4.Tick, eqptime5.Tick
        Dim timername As String

        tmr1 = tmr1 + 1
        eqptimer1_4.Text = tmr1
        If tmr1 = 10 Then
            e1a = e1a + 1
            tmr1 = 0
        End If
        If e1a = 60 Then
            e1b = e1b + 1
            e1a = 0
        End If
        If e1b = 60 Then
            e1c = e1c + 1
            e1b = 0
        End If
        eqptimer1_3.Text = e1a
        eqptimer1_2.Text = e1b
        eqptimer1_1.Text = e1c
    End Sub
 
I assume you are trying to assign the timername in the blank line as you define the variable???

If so, try:

Code:
Dim timername As String
timername = DirectCast(sender, Timer).Tag.ToString

Are you doing anything else with timername in this procedure call??? Or just storing the name of the timer that called the code?

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Code:
Public Class MyTimer
	Inherits System.Windows.Forms.Timer


	Private FName As String = String.Empty

	Public Property Name() As String
		Get
			Return FName
		End Get
		Set(ByVal value As String)
			FName = value
		End Set
	End Property

End Class

This gives you a proper Name property and then you can use:

Code:
Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		MyTimer1.Start()

	End Sub

	Private Sub MyTimer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyTimer1.Tick

		MyTimer1.Stop()
		MessageBox.Show(CType(sender, MyTimer).Name)

	End Sub
End Class

Just replace your current Timers with MyTimers.

[vampire][bat]
 
E&F shows us the graceful method of doing this....Thanks. I should have thrown that out there as an option as well. Have a star from me.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
mstrmage1768, thanks. I'm just embarrassed that I'd assumed there already was a Name property.

[vampire][bat]
 
I think we're getting close and I really appreciate this immediate response. I'm probably doing something wrong now, but when I try to simply display the timer name in a message box, I get a "Specified cast is not valid" unhandled exception.

I created a class.vb file, which contains the following...

Code:
Public Class MyTimer

    Inherits System.Windows.Forms.Timer

        Private FName As String = String.Empty

        Public Property Name() As String
            Get
                Return FName
            End Get
            Set(ByVal value As String)
                FName = value
            End Set
        End Property

End Class

And then simply tried...

Code:
MessageBox.Show(CType(sender, MyTimer).Name)

But when the tick event of one of the timers fires I get this "Specified cast is not valid" message.
 
Did you replace your current timers with the MyTimer class?


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Okay, I've replaced the current timers with the MyTimer class, and am no longer getting the "Specified cast is not valid" error, but my message box is empty when I try to dispaly the name of the timer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top