-
Jun 26th, 2022, 04:27 PM
#1
Thread Starter
New Member
Reference to timer component on UserControl
Hello every one
Glad to join VB Forums, this is my first post.
I have a form containing multiple buttons and a panel
I have created several simple UserControls, each has few labels and a timer component
Every button will add at runtime an instance of a specific UserControl.
Now, in the main form, How can I access the timer of the added UserControl?
Appreciate your guidance
-
Jun 26th, 2022, 10:02 PM
#2
Re: Reference to timer component on UserControl
If you added the Timer via the designer then, just like controls, the corresponding field will be Public by default, so you can already access it. If you added it via code then you would need to assign it to a Public field or property yourself.
That said, you shouldn't access it directly anyway. When you add controls and components in the designer, you should change their access level to Private so they specifically cannot be access from outside. You should provide pass-through members for the specific functionality you need. For instance, if you need to be able to start and stop the Timer from the outside as well as be notified when it Ticks then you would provide appropriate pass-through methods and events:
vb.net Code:
Private WithEvents timer1 As New Timer With {.Interval = 1000}
Public Sub StartTimer()
timer1.Start()
End Sub
Public Sub StopTimer()
timer1.Stop()
End Sub
Public Event TimerTick As EventHandler
Protected Overridable Sub OnTimerTick(e As EventArgs)
RaiseEvent TimerTick(Me, e)
End Sub
Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
OnTimerTick(EventArgs.Empty)
End Sub
-
Jun 27th, 2022, 03:40 AM
#3
Thread Starter
New Member
Re: Reference to timer component on UserControl
Originally Posted by jmcilhinney
If you added the Timer via the designer then, just like controls, the corresponding field will be Public by default, so you can already access it. If you added it via code then you would need to assign it to a Public field or property yourself.
That said, you shouldn't access it directly anyway. When you add controls and components in the designer, you should change their access level to Private so they specifically cannot be access from outside. You should provide pass-through members for the specific functionality you need. For instance, if you need to be able to start and stop the Timer from the outside as well as be notified when it Ticks then you would provide appropriate pass-through methods and events:
vb.net Code:
Private WithEvents timer1 As New Timer With {.Interval = 1000}
Public Sub StartTimer()
timer1.Start()
End Sub
Public Sub StopTimer()
timer1.Stop()
End Sub
Public Event TimerTick As EventHandler
Protected Overridable Sub OnTimerTick(e As EventArgs)
RaiseEvent TimerTick(Me, e)
End Sub
Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
OnTimerTick(EventArgs.Empty)
End Sub
Thank you jmcilhinney, appreciate it
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
|