Solved

Draw lines based on a timer using GDI

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Crystal reports vb.net 2 54
Disable extension 8 48
write xml in vb.net 2 33
Toolbar to format all windows forms controls at design time needed 5 16
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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