Results 1 to 3 of 3

Thread: Reference to timer component on UserControl

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2022
    Posts
    2

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. Private WithEvents timer1 As New Timer With {.Interval = 1000}
    2.  
    3. Public Sub StartTimer()
    4.     timer1.Start()
    5. End Sub
    6.  
    7. Public Sub StopTimer()
    8.     timer1.Stop()
    9. End Sub
    10.  
    11. Public Event TimerTick As EventHandler
    12.  
    13. Protected Overridable Sub OnTimerTick(e As EventArgs)
    14.     RaiseEvent TimerTick(Me, e)
    15. End Sub
    16.  
    17. Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
    18.     OnTimerTick(EventArgs.Empty)
    19. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2022
    Posts
    2

    Re: Reference to timer component on UserControl

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. Private WithEvents timer1 As New Timer With {.Interval = 1000}
    2.  
    3. Public Sub StartTimer()
    4.     timer1.Start()
    5. End Sub
    6.  
    7. Public Sub StopTimer()
    8.     timer1.Stop()
    9. End Sub
    10.  
    11. Public Event TimerTick As EventHandler
    12.  
    13. Protected Overridable Sub OnTimerTick(e As EventArgs)
    14.     RaiseEvent TimerTick(Me, e)
    15. End Sub
    16.  
    17. Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
    18.     OnTimerTick(EventArgs.Empty)
    19. 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
  •  



Click Here to Expand Forum to Full Width