Setting up a global timer - accesseable between child objects

Posted on 2006-03-26
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
Question by:Null_Coder
    LVL 14

    Accepted Solution

    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


    Author Comment

    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

    Author Comment

    Solid!  its working great.  

    Thank you very much for the help!
    LVL 14

    Expert Comment

    You're welcome, have fun coding!

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now