Guys:

Please help.

I need to create an on-screen shape (circle) that has a fill color, which will blink every X seconds (go from fill color, to white, back to fill color). This is trivial to do...BUT... i want to do it in my own class, so that I can instantiate it numerous times passing it unique coordinates for each instantiation.

How do I do it? Can a class have its own "timer control" in it independent from the form? How would I draw a shape from a class? Any ideas?

I am fairly proficient in VB6, still poking my way through .NET stuff, so don't laugh too much at my question :)

Thanks in advance,

Recommended Answers

All 2 Replies

See if this helps.

Public Class Form1
    Private cls As New myCoolClass '// get access to your Class.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cls.myTimer.Enabled = Not cls.myTimer.Enabled '// toggle timer on/off.
    End Sub
End Class

Public Class myCoolClass
    '// WithEvents, to get the Events in the right ComboBox, when selecting your object in the left ComboBox of the code window.
    Public WithEvents myTimer As New Timer With {.Interval = 1000} '// 1000 is 1 second.
    '// found in the right ComboBox of top of code window.
    Private Sub myTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTimer.Tick
        drawCoolShape(Form1) '// Call the drawCoolShape Sub.
    End Sub
    '// Draw Shape.
    Private Sub drawCoolShape(ByVal selectedForm As Form)
        Static bToggleColor As Boolean = True '// Toggles the colors of the pen the shape is drawn with.
        Dim myGraphics As Graphics = selectedForm.CreateGraphics
        Dim myPen As Pen '// pen to draw with.
        '// set pen color and width.
        If bToggleColor = True Then myPen = New Pen(Color.SteelBlue, 10) Else myPen = New Pen(Color.Orange, 10)
        myGraphics.DrawEllipse(myPen, 50, 50, 10, 10) '// (preset pen, location.X, location.Y, width, height)
        bToggleColor = Not bToggleColor '// Toggle the Boolean True/False.
    End Sub
End Class

I added the Class on the same code page as Form1.
You can also add a Class as a separate Item and use the posted code in the same way.

Whiz:

You are awesome ! worked exactly as defined. Perfect.

I am amazed you were able to do that so quickly (still feeling my way around).

Thank you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.