Question Draw a line

solfinker

Well-known member
Joined
Aug 6, 2013
Messages
71
Programming Experience
Beginner
Hello :peaceful:

I want to draw a line within a Timer_Tick, this is, a Sub within another Sub:


VB.NET:
Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Dim blackPen As New Pen(Color.Black, 3)
e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
End Sub
End Sub
and as you can imagine this does not work.
I don't know much about vb.net - that's for sure - but I'm thinking about using the following code:
VB.NET:
Dline.dlinexy(e,x1, y1, x2, y2)

Public Class Dline
    Public Shared Function dlinexy(ByVal e As PaintEventArgs, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) As Single
        Dim blackPen As New Pen(Color.Black, 3)
        e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
        Return (x1)  'useless
    End Function
End Class
that gives me the error message:
You cannnot convert an object of the kind 'System.EventArgs' to the kind 'System.Windows.Forms.PaintEventArgs'

I suppose there has to be a simpler way to draw a line within a Timer_Tick event.

Thank you for your help
 
Last edited:
You should draw the line in a Paint event handler, for example:
VB.NET:
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim blackPen As New Pen(Color.Black, 3)    
        e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
        blackPen.Dispose
End Sub
In your Timer Tick event handler, you set the values of x1, y1, x2 and y2 to whatever new values you want to give them, then call Me.Refresh. That causes the Paint event to fire, and it will be handled by the Form1_Paint sub. This is necessarily to allow the OS to keep some control over painting on the screen, so your program's display will not be corrupted by other programs.

The above code assumes you are drawing on a Form. If you do that, set the Form's DoubleBuffered property to True to avoid flickering. That's not necessary if you use a PictureBox for drawing. In that case you should Handle PictureBoxName.Paint and call PictureBoxName.Refresh.

VicJ
 
Thank you very much.

I have several forms.
If I write the code on the main form, it does not work:
VB.NET:
Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
 ...
 FormCardio.Refresh()
End Sub
Private Sub FormCardio_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim blackPen As New Pen(Color.Black, 3)
        e.Graphics.DrawLine(blackPen, formCardio.os11.Left, formCardio.os11.Top, formCardio.os12.Left, formCardio.os12.Top)
        blackPen.Dispose()
End Sub

Then I have tried:

VB.NET:
Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
 ...
 FormCardio.Refresh()
End Sub
and then in the FormCardio code:

VB.NET:
Private Sub FormCardio_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim blackPen As New Pen(Color.Black, 3)
        e.Graphics.DrawLine(blackPen, Me.os11.Left, Me.os11.Top, Me.os12.Left, Me.os12.Top)
        blackPen.Dispose()End Sub

and it works great.

Thank you.
 
You have the right idea. "Me" refers to the form that contains the code. For another form, you have to give the name of the form. BB
 
If I write the code on the main form, it does not work:

You don't write code in one for to draw on another form. Each form would handle its own Paint event and draw on itself. If you need to prompt a repaint from another form then you can call the target form's Refresh method, which invalidates its entire surface and then raises the Paint event. If you need to pass data to the form for it to use to perform the drawing then you pass that data in the same way you pass any data in, i.e. set one or more properties or call a method and pass one or more arguments.
 

Latest posts

Back
Top