Tuesday, January 10, 2006

Draw a line at design time in Visual Studio

In Visual Basic 6.0 there was a Line control that could be used to draw a line on a form. In VBA, VB.NET, Visual Basic 2005 and other similar programs there is no line control that you can use. The most lightweight and easy thing to do is to use a Label control. Set Height or Width to 1 depending on if you want the line horizontal or vertical Clear the Caption / Text Set the BorderStyle to None Set AutoSize to False Set the BackColor to the color of the line If you want a thicker line increase the height or width Now if you want to draw a diagonal line you have to code a little
' Visual Basic 2005
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    ' Draw a diagonal line from the top left to the lower right.
    e.Graphics.DrawLine(Pens.Black, 0, 0, Me.ClientSize.Width, _
    Me.ClientSize.Height)
End Sub

5 comments:

  1. Restored comment
    Anonymous said...

    Thank you. This was clever and very helpful to me for my VB.NET 2005 Windows Application.
    August 28, 2007

    Anonymous said...

    Hello.. I want to do the same but not in the form load.. I want to draw the line by clicking on a button. Thanks for your help.
    March 16, 2008

    JTB World said...

    To draw the line on a click on a button just add the code to that event instead.
    March 16, 2008

    Anonymous said...

    JTB World,

    Thats a good suggestion IF you are doing WinForms work in VS2005, but if you are doing ASP.NET pages, there is no AutoSize in Labels or Textboxes. Which leaves me trying to find a simple way to draw a line on my ASP.NET page.

    With no disrespect to your good advice - why the heck did MS leave such a basic tool as a line OUT of VS2005!!! I cant stand writing lines of code to draw a line!!! OUCH!!!

    Hmmm, progress - ya gotta love it - two steps forward, five back...
    June 24, 2008

    ReplyDelete
  2. hmmm why not doing a simple response with a hr hml tag to draw a line in asp.net?

    ReplyDelete
  3. Thank you very much...
    i didnt expect this easier way of drawing line in the form:)

    ReplyDelete
  4. Thanks for guider. It is I find

    ReplyDelete
  5. Thank you ...

    ReplyDelete

Some of the latest blog posts

Subscribe to RSS headline updates from:
Powered by FeedBurner