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


Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

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 in Visual Studio.

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:

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

                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);

Introduction to the Line and Shape Controls
How to: Draw Shapes with the OvalShape and RectangleShape Controls