How to draw a simple line

marty44

New member
Joined
Mar 2, 2009
Messages
2
Programming Experience
10+
Hi, as an old time VB Pro coder I'm new to Visual Studio.Net. Having trouble learning how to draw a simple line on a Windows Form. Any assistance would be welcome.
 
I tried this example from the help doc, but it won't work. I'm sure it's a simple mistake. I created a form called form1 with a command control called button1. When I click the button I try to invoke the drawline method where it fails.



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call DrawLineFloat(e)
End Sub
Public Sub DrawLineFloat(ByVal e As PaintEventArgs)
' Create pen.
Dim blackPen As New Pen(Color.Black, 3)
' Create coordinates of points that define line.
Dim x1 As Single = 5
Dim y1 As Single = 10
Dim x2 As Single = 10
Dim y2 As Single = 20
' Draw line to screen.
e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
End Sub
End Class
 
Hi marty44,

I tried this example from the help doc, but it won't work. I'm sure it's a simple mistake. I created a form called form1 with a command control called button1. When I click the button I try to invoke the drawline method where it fails.



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call DrawLineFloat(e)
End Sub
Public Sub DrawLineFloat(ByVal e As PaintEventArgs)
' Create pen.
Dim blackPen As New Pen(Color.Black, 3)
' Create coordinates of points that define line.
Dim x1 As Single = 5
Dim y1 As Single = 10
Dim x2 As Single = 10
Dim y2 As Single = 20
' Draw line to screen.
e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
End Sub
End Class

As far as I see in your code, with "Call DrawLineFloat(e)", you're tring to send e object ( which is System.EventArgs ) to DrawLineFloat sub and use it there as PaintEventArgs. This is not right.

Get rid of ther button and try placing your DrawLineFloat function with in Form Paint event. This will draw your line.
 
Back
Top