How to: Draw Lines with the LineShape Control (Visual Studio)

You can use the LineShape control to draw horizontal, vertical, or diagonal lines on a form or container, both at design time and at run time.

Note Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings.

To draw a line at design time

  1. Drag the LineShape control from the Visual Basic PowerPacks tab in the Toolbox drag to a form or container control.

  2. Drag the sizing and move handles to size and position the line.

    You can also size and position the line by changing the X1, X2, Y1, and Y2 properties in the Properties window.

  3. In the Properties window, optionally set additional properties such as BorderStyle or BorderColor to change the appearance of the line.

To draw a line at run time

  1. On the Project menu, click Add Reference.

  2. In the Add Reference dialog box, select Microsoft.VisualBasic.PowerPacks.VS, and then click OK.

  3. In the Code Editor, add an Imports or using statement at the top of the module:

    Imports Microsoft.VisualBasic.PowerPacks
    
    using Microsoft.VisualBasic.PowerPacks;
    
  4. Add the following code in an Event procedure:

    Dim canvas As New ShapeContainer
    Dim theLine As New LineShape
    ' Set the form as the parent of the ShapeContainer.
    canvas.Parent = Me 
    ' Set the ShapeContainer as the parent of the LineShape.
    theLine.Parent = canvas
    ' Set the starting and ending coordinates for the line.
    theLine.StartPoint = New System.Drawing.Point(0, 0)
    theLine.EndPoint = New System.Drawing.Point(640, 480)
    
    ShapeContainer canvas = new ShapeContainer();
    LineShape theLine = new LineShape();
    // Set the form as the parent of the ShapeContainer.
    canvas.Parent = this;
    // Set the ShapeContainer as the parent of the LineShape.
    theLine.Parent = canvas;
    // Set the starting and ending coordinates for the line.
    theLine.StartPoint = new System.Drawing.Point(0, 0);
    theLine.EndPoint = new System.Drawing.Point(640, 480);
    

See Also

Tasks

How to: Draw Shapes with the OvalShape and RectangleShape Controls (Visual Studio)

Reference

LineShape

Concepts

Introduction to the Line and Shape Controls (Visual Studio)