We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Setting up a global timer - accesseable between child objects

Null_Coder asked
Medium Priority
Last Modified: 2010-04-23
I am trying to create a program where there are a bunch of custom picture box objects (that I created) which can access a shared timer.  I tried to create the timer on the main form and have the PBs call back to a function to start / stop the timer, but when I try to call back to the parent form its just not working.

Me.Parent.stopTimer is not an option
nor is Me.ParentForm.StopTimer (Or the start versions of these)

Anyone know how to get this to work?

Here is the code that I am using:

Public Class Main

    Private m_clsTimer As System.Timers.Timer
    Private childIcon As PBObject

  Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        m_clsTimer = New System.Timers.Timer(1500)
        m_clsTimer.SynchronizingObject = Me
        m_clsTimer.AutoReset = False
        AddHandler m_clsTimer.Elapsed, AddressOf Timer_Elapsed

        Dim PB1 As New PBObject()
        PB1.setupIcon("C:\gif\Accessibility-Wizard.gif", 100, 100, Me)
        Dim PB2 As New PBObject()
        PB2.setupIcon("C:\gif\ActiveX-Control-Test-Contai.gif", 200, 100, Me)

        PB1.Visible = True
    End Sub

   Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        ' release timer...
        ' timer has elapsed, do more stuff...
        MsgBox("timer over man! " & childIcon.Name)
    End Sub

    Public Sub stopTimer(ByVal Caller As PBObject)
        childIcon = Nothing
        m_clsTimer.Stop() ' Should this have the dispose / close code here
    End Sub

    Public Sub StartTimer(ByVal Caller As PBObject)
        childIcon = Caller
        m_clsTimer.Stop() ' Do I want to dispose / Close this and start a new timer somehow?
    End Sub

And now for the PB object Class

Imports System
Imports System.Drawing

Public Class PBObject
    Inherits PictureBox

    Private NumberOfChildren = 0
    Private childrenArray(10) As PBObject
    Private ParentX As Integer = -1
    Private ParentY As Integer = -1
    Private ParentObj As PBObject
    Public HideIcon As Boolean = True ' Should only be false while the mouse is over an icon
    Private parentForm As Windows.Forms.Form

    Public Sub setupIcon(ByVal IconLocation As String, ByVal LocationX As Integer, ByVal LocationY As Integer, ByRef setForm As Windows.Forms.Form)
        Me.Name = IconLocation
        Me.Image = Drawing.Image.FromFile(IconLocation)
        Me.Left = LocationX
        Me.Top = LocationY
        Me.Visible = False
        Me.SizeMode = PictureBoxSizeMode.StretchImage
        parentForm = setForm
    End Sub

    Public Sub AddChild(ByVal ChildIcon As PBObject)
        ' Add children Icons to this node
        ' try to keep them in order incase it maters later on
        childrenArray(NumberOfChildren) = ChildIcon
        NumberOfChildren += 1
        ChildIcon.setParent(Me, Me.Top, Me.Left)
    End Sub

    Public Sub setParent(ByVal Parent As SpyderconObject, ByVal topP As Integer, ByVal leftP As Integer)
        ParentX = leftP
        ParentY = topP
        ParentObj = Parent
    End Sub

    Private Sub PBObject_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
         'Stop Timer!
    End Sub

    Private Sub PBObject_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
        ' Stop Existing Timer
        ' Start a new timer

    End Sub

    Public Sub DrawLineToParent()
        ' This function works pretty well. Might want to change the coords slightly though.
        If ParentX <> -1 And ParentY <> -1 Then
            ' draw line on parent form to parent object
            Dim clsParentGraphics As System.Drawing.Graphics = Me.Parent.CreateGraphics()
            clsParentGraphics.DrawLine(Pens.Red, Me.Left, Me.Top, ParentX, ParentY)
        End If
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        ' this is not working - I dont want to override the on paint function, but to
        ' have this code run in addition to the drawing function. Right now it draws the
        ' lines but the icons appear as grey blocks instead of actual icons.
        ' - Need to start some google searches on it.

        If Me.Visible = True Then
        End If
    End Sub
Watch Question

Try to create a delegate in your class to pass the address of your StartTimer and StopTimer function

public delegate Sub StopTimer ()
public delegate Sub StartTimer

create a member var.
private _stopTimer as StopTimer
private _startTimer as StartTimer

and then in your sub New of your class:
public sub New (start as StartTimer, stop as StopTimer)
_startTimer = start
_stopTimer = stop
end sub

init your class like :
dim myObj as new PBObject(addressof StartTimer, Addressof StopTimer)

And then define the startTimer end stopTimer subs in your mainForm

something like this

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Previously I tried using asyncrous delegates and a sleep call which didnt work so well.  If a user entered and left a bunch of PBs, the timeing between them would overlap.

I'll give this a shot though and see what happens


Solid!  its working great.  

Thank you very much for the help!
You're welcome, have fun coding!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.