Draw lines based on a timer using GDI

I can draw lines using on Paint event when the form loads with no problem.

    Private Sub Navigation_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawLine(Pens.Blue, 216, 160, 216, 220)
        e.Graphics.DrawLine(Pens.Blue, 216, 220, 243, 220)
    End Sub

However, I would like the lines to be created and added to the form using a timer.  Thanks for the help.
rogue_1Asked:
Who is Participating?
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.

doraiswamyCommented:
Some issues here:
a) How would the timer know the end points of the line?
b) The paint event would still have to draw the lines, as otherwise what is drawn would not be redrawn if the form is resized or obscured by another form. So you have to maintain a list of lines and draw these inside the OnPaint event.
c) The timer event creates the line and adds it to the list of lines to be painted. It then calls Invalidate.

To use a timer, drag it from the toolbox onto your form.
Use Timer1.interval to set the duration between event firing.
Enable the timer.
In the Timer1_Tick event, create the line to be drawn and add it to a collection of lines and then call Invalidate.

In the OnPaint event, draw all the lines in the collection.
0
rogue_1Author Commented:
I actually don't need to draw the line over time.  Each line needs to be drawn in 5 second intervals.  Here is the code I have so far:

    Private Sub Navigation_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawLine(Pens.Blue, 216, 160, 216, 220)
        e.Graphics.DrawLine(Pens.Blue, 216, 220, 243, 220)
        e.Graphics.DrawLine(Pens.Gray, 284, 138, 619, 138)
        e.Graphics.DrawLine(Pens.Gray, 619, 138, 619, 326)
        e.Graphics.DrawLine(Pens.Red, 376, 330, 376, 340)
        e.Graphics.DrawLine(Pens.Red, 376, 340, 533, 340)
        e.Graphics.DrawLine(Pens.Gold, 575, 202, 575, 327)
        e.Graphics.DrawLine(Pens.Orange, 336, 239, 336, 290)
        e.Graphics.DrawLine(Pens.MediumOrchid, 475, 186, 396, 186)
        e.Graphics.DrawLine(Pens.MediumOrchid, 396, 186, 396, 290)
        e.Graphics.DrawLine(Pens.LawnGreen, 239, 160, 239, 176)
        e.Graphics.DrawLine(Pens.LawnGreen, 239, 176, 475, 176)
        e.Graphics.DrawLine(Pens.Ivory, 277, 239, 277, 350)
        e.Graphics.DrawLine(Pens.Ivory, 277, 350, 533, 350)
        e.Graphics.DrawLine(Pens.Chocolate, 584, 366, 584, 394)
        e.Graphics.DrawLine(Pens.Chocolate, 584, 394, 229, 394)
        e.Graphics.DrawLine(Pens.DarkOrange, 131, 370, 131, 144)
        e.Graphics.DrawLine(Pens.DarkOrange, 131, 144, 164, 144)
        e.Graphics.Dispose()
    End Sub

    Private Sub Navigation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
        Timer1.Interval = 1000
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Code to invoke the Paint even
        'Draw one line at a time after a 5 second interval
        Me.Invalidate()
        Timer1.Stop()
    End Sub

The items I am not grasping about drawing is:
(a) If the lines are added to the paint function on the form, wouldn't they show up when the form loads? I would like the form to load initially without the lines.
(b) Once I understand the first part, how do I call the paint function from the Timer1_Tick event or any other event for that matter.

Thanks for the help.
0
doraiswamyCommented:
If I understand you correctly, then you want the lines to paint one at a time at 5 second intervals after the form loads. Thereafter, they should remain on the form. So when the form loads, there should be nothing on it.

The OnPaint event fires every time there is a change in the way the form appears, be it the size changing, or if anything new is drawn on it. The paint event can be fired by "Invalidating" the form.

Here's some sample code which I tested:

Public Class Form1
    Class line
        Dim PenColor As Pen
        Dim LineStart As Point
        Dim LineEnd As Point
        Public Sub New(ByVal lpen As Pen, ByVal startpt As Point, ByVal endpt As Point)
            PenColor = lpen
            LineStart = startpt
            LineEnd = endpt
        End Sub
        Public Sub New(ByVal lpen As Pen, ByVal x1 As Short, ByVal y1 As Short, ByVal x2 As Short, ByVal y2 As Short)
            PenColor = lpen
            LineStart = New Point(x1, y1)
            LineEnd = New Point(x2, y2)
        End Sub
        Public Sub Draw(ByVal gr As Graphics)
            gr.DrawLine(PenColor, LineStart.X, LineStart.Y, LineEnd.X, LineEnd.Y)
        End Sub
    End Class
    Dim LinesOnForm As New Collection
    Dim LinesToShow(2) As line      ' set this dimension to 1 less than the number of lines you want to display
    Dim currentline = 0


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LinesToShow(0) = New line(Pens.Blue, 0, 0, 50, 40)
        LinesToShow(1) = New line(Pens.Gray, 20, 20, 100, 150)
        LinesToShow(2) = New line(Pens.Red, 50, 60, 125, 250)
        ' etc. etc.
        Timer1.Interval = 1000      ' set this accordingly
        Timer1.Enabled = True
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        LinesOnForm.Add(LinesToShow(currentline))
        currentline += 1
        If currentline > LinesToShow.GetUpperBound(0) Then Timer1.Stop()
        Invalidate()
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        For i As Integer = 1 To LinesOnForm.Count
            Dim myline As line = CType(LinesOnForm.Item(i), line)
            myline.Draw(e.Graphics)
        Next
    End Sub
End Class
0

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
rogue_1Author Commented:
Thanks Dora.  That example works great and now I can give me form some life.
0
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.