Solved

Draw lines based on a timer using GDI

Posted on 2007-03-27
4
255 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now