Draw lines based on a timer using GDI

Posted on 2007-03-27
Medium Priority
Last Modified: 2008-03-06
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.
Question by:rogue_1
  • 2
  • 2

Assisted Solution

doraiswamy earned 2000 total points
ID: 18806364
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.

Author Comment

ID: 18807384
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)
    End Sub

    Private Sub Navigation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        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
    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.

Accepted Solution

doraiswamy earned 2000 total points
ID: 18808145
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
    End Sub

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

Author Comment

ID: 18808404
Thanks Dora.  That example works great and now I can give me form some life.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…

624 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