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 TouchToneTommy 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.???
 
You will need to fill in the NAME property for each of the timers in the properties window. This is a property you have added and it is not pulling the system "name" of the timer.

=======================================
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
 
Further apologies. Just providing a formal Name property doesn't work either. It seems that Timer is not regarded as a control.

So lets disguise it:

Code:
Public Class MyTimer
	Inherits Control

	Private WithEvents FTimer As System.Windows.Forms.Timer

	Public Shadows Property Enabled() As Boolean
		Get
			Return FTimer.Enabled
		End Get
		Set(ByVal value As Boolean)
			FTimer.Enabled = value
		End Set
	End Property

	Public Property Interval() As Integer
		Get
			Return FTimer.Interval
		End Get
		Set(ByVal value As Integer)
			FTimer.Interval = value
		End Set
	End Property


	Public Sub Start()

		FTimer.Start()

	End Sub

	Public Sub [Stop]()

		FTimer.Stop()

	End Sub


	Public Event Tick(ByVal sender As MyTimer, ByVal e As System.EventArgs)

	Public Sub New()

		FTimer = New System.Windows.Forms.Timer


	End Sub

	Private Sub FTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles FTimer.Tick

		RaiseEvent Tick(Me, e)

	End Sub
End Class

and to test it:

Code:
Public Class Form1

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

		Dim ButtonSuffix As String = CType(sender, Button).Name.Substring("Button".Length)

		For Each c As Control In Controls
			If TypeOf c Is MyTimer Then
				If c.Name.EndsWith(ButtonSuffix) Then
					CType(c, MyTimer).Start()
					Exit Sub
				End If
			End If
		Next

	End Sub

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

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

	End Sub
End Class

To give the Timer the appearance of a control I've used "indirection". In other words, I've created a basic control add added the main Timer properties, methods and the Tick event (and a hidden timer, of course).

I think this is it, this time.

[vampire][bat]
 
Two things:

1 - MyTimer WILL NOT appear in the Component Tray, it will be an "invisible" control on the form.

2 - The Timer is a component and when I tried to inherit from component instead of control, the designer reported (amongst dozens of other errors), that components don't have a Name property.

[vampire][bat]
 
I've just noticed that I left some testing code in place so a couple of changes should be made - they are not necessary, but are more compatible with the way in which .Net does things.


Code:
   Public Event Tick(ByVal sender As [s]MyTimer[/s] [b]Object[/b], ByVal e As System.EventArgs)

    Public Sub New()

        FTimer = New System.Windows.Forms.Timer


    End Sub


and

Code:
    Private Sub MyTimer1_Tick(ByVal sender As [s]MyTimer[/s] [b]Object[/b], ByVal e As System.EventArgs) Handles MyTimer1.Tick, MyTimer2.Tick, MyTimer3.Tick

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

    End Sub


You need to change both or neither.

[vampire][bat]
 
I'm intrigued as to what you're trying to achieve by this.....

C
 
Craig, I'm not sure who you are targetting with your comment.

My understanding of the problem was that there was a need for a mechanism to identify a specific Timer within the Tick event and elsewhere in the program. Neither the sender parameter nor the Controls collection provided a suitable mechanism. As such I produced a simple control, that, from the point of view of the programmer using it was a Timer with a fully functional Name property and visibility via the Controls collection.

[vampire][bat]
 
I meant Fletch12. It just strikes me as an odd thing to want to do. And odd things to do usually mean that there is something fishy somewhere way back in the design.

I guess i'm trying to see whether you've given a neat solution to the wrong question.

C
 
My intention of this was to create one sub routine that handles the tick event of multiple timers on my form. However, I needed to know which timer was called, and thought using the name was the way to do it. This would be much less code, compared to a sub routine for each timer I have.
 
But what Craig and I think the rest of us is trying to figure out is what are you trying to do with the information of which timer called the routine?????

What is you actual process and what are the expect results?? Such as:
I have created one Tick routine for several timers. I need to know which Timer executed the Tick routine so I can Stop that Timer if a certain condition has been met.

As Craig has eluded to, E&F and I have been trying to help you but we don't seem to be giving you an answer that works for you.

So we need more information about what you are trying to accomplish.

=======================================
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'll go further.

From what I can see, you're trying to use multiple timers to create a display. But I think it could be created, from what I can see, by a single timer.

And at no point in the code posted is the name referenced.

It all looks hooked right.

So, i'll ask a little more bluntly. What is the problem you are trying to solve?

C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top