Setting up a global timer - accesseable between child objects

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.AddChild(PB2)
        PB1.AddChild(PB3)
...
        PB1.Visible = True
        Me.Controls.Add(PB1)
        Me.Controls.Add(PB2)
...
    End Sub

   Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        ' release timer...
        m_clsTimer.Close()
        m_clsTimer.Dispose()
        ' 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?
        m_clsTimer.Start()
    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
        Me.Refresh()
    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)
            clsParentGraphics.Dispose()
        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
            DrawLineToParent()
        End If
    End Sub
Null_CoderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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



e.g.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Null_CoderAuthor Commented:
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
Null_CoderAuthor Commented:
Solid!  its working great.  

Thank you very much for the help!
PockyMasterCommented:
You're welcome, have fun coding!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.