[Webinar] Learn how to a build a cloud-first strategyRegister Now


Setting up a global timer - accesseable between child objects

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

Accepted Solution

PockyMaster earned 500 total points
ID: 16295203
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

ID: 16295290
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

ID: 16295684
Solid!  its working great.  

Thank you very much for the help!
LVL 14

Expert Comment

ID: 16297512
You're welcome, have fun coding!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

868 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