Solved

Draw lines based on a timer using GDI

Posted on 2007-03-27
4
271 Views
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.
0
Comment
Question by:rogue_1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:doraiswamy
doraiswamy earned 500 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.
0
 

Author Comment

by:rogue_1
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)
        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
 
LVL 8

Accepted Solution

by:
doraiswamy earned 500 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
        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
 

Author Comment

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

Featured Post

Independent Software Vendors: 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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

632 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