How to draw an line on a form in VB.NET?
In vb6 we had a simple Control for that... Anyone can tell me how?
Printable View
How to draw an line on a form in VB.NET?
In vb6 we had a simple Control for that... Anyone can tell me how?
First you create a pen object to draw the line with. Then you draw the line with the CreateGraphics.DrawLine method, sending the pen, starting and ending points as arguments.
Example:
VB Code:
Dim blackPen As New Pen(Color.Black) CreateGraphics.DrawLine(blackPen, 0, 0, 50, 50)
/Sara
Be sure to import System.Darwing
then one simple way maybe this
VB Code:
Dim g As Graphics = Me.CreateGraphics Dim p As Pen = New Pen(Color.Black) g.DrawLine(p, 0, 0, 200, 200) g.Dispose() p.Dispose()
Thanks for the reaction, but none of the examples work... There aren't any lines drawn on my form
/edit
Aargh, it does work... only in the Form_Paint Sub, or am i wrong?
Maybe you are putting the code in form load event, I dont know y it does not draw in form load, I will tell you if i find the reason.
Ok, I find out the reason,
When you draw a line in form load event, at first the line is drwan then the form paints, so it earases the line. You should put it in onPaint event if you want to draw the line when form loads.
VB Code:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) e.Graphics.DrawLine(Pens.Black,0,0,50,50) End Sub