Furqan Sehgal
Furqan Sehgal

Reputation: 4997

Drawing line in vb.net

I am trying to use the following code in a button. I want to call DrawLineFloat. I tried call DrawLineFloat() but it did not work. What do I need to enter in ()?

Thanks

 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 = 100.0F
    Dim y1 As Single = 100.0F
    Dim x2 As Single = 500.0F
    Dim y2 As Single = 100.0F
    ' Draw line to screen.
    e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
End Sub

Upvotes: 1

Views: 20973

Answers (3)

Steve
Steve

Reputation: 216286

I suppose you are calling this inside a form, but not in the Paint event. So you need to create the Graphics used to draw your line

    Public Sub DrawLineFloat() 
        ' Create pen. 
        Dim blackPen As New Pen(Color.Black, 3) 
        ' Create coordinates of points that define line. 
        Dim x1 As Single = 100.0F 
        Dim y1 As Single = 100.0F 
        Dim x2 As Single = 500.0F 
        Dim y2 As Single = 100.0F 
        ' Draw line to screen. 

        Dim g As Graphics = Me.CreateGraphics()
        g.DrawLine(blackPen, x1, y1, x2, y2) 

        blackPen.Dispose()

End Sub 

Also note that the Pen object should be disposed as soon as possible

Upvotes: 4

Dipesh Bhatt
Dipesh Bhatt

Reputation: 825

try passing null value (Nothning in VB) to the function.

DrawLineFloat(Nothing)

Upvotes: -1

PraveenVenu
PraveenVenu

Reputation: 8337

Why not write like

Public Sub DrawLine()
    ' Create pen.
    Dim blackPen As New Pen(Color.Black, 3)
    ' Create coordinates of points that define line.
    Dim x1 As Single = 100.0F
    Dim y1 As Single = 100.0F
    Dim x2 As Single = 500.0F
    Dim y2 As Single = 100.0F
    ' Draw line to screen.
    e.Graphics.DrawLine(blackPen, x1, y1, x2, y2)
End Sub

and call like

DrawLine()

Upvotes: 0

Related Questions