Alex Gordon
Alex Gordon

Reputation: 60691

how do i draw a line on a form?

in vb.net i would like to draw a regular line on a form. is there a control to do this?

Upvotes: 7

Views: 46860

Answers (3)

drdavem
drdavem

Reputation: 21

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    e.Graphics.DrawLine(Pens.Black, New Point(0, Me.Height - 1), New Point(Me.Width, Me.Height - 1))
End Sub

This draws a line on the bottom of the control every time it is painted.

Upvotes: 2

Mosquito Mike
Mosquito Mike

Reputation: 982

What Mitch Wheat said is generally regarded as the correct answer, and what I have done in the past. However, if you want to have a visual control that you can drag on to a form, add the Microsoft.VisualBasic.PowerPack to your visual studio toolbox. To do that right-click on toolbox select "Choose Items...". Locate "Line Shape" on the .Net Framework Components tab.

Upvotes: 18

Mitch Wheat
Mitch Wheat

Reputation: 300489

One way at design time is to use a Label control and set it's height, or width to 1 (2px and 3D border gives a nice chiseled effect). Or else you can manually draw using GDI:

Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics as System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawLine(myPen, 0, 0, 200, 200)
myPen.Dispose()
formGraphics.Dispose()

Upvotes: 10

Related Questions