Lesson 27

Drawing Rectangles

In this lesson, you will learn how to draw rectangles in Visual Basic 2015 using the Graphics object, the Pen object, and the DrawRectangle method.

27.1 Introduction

In the previous lesson, you learned how to create the Graphics object and the Pen object to draw straight lines. Now you will learn how to draw rectangles in Visual Basic 2015. :contentReference[oaicite:1]{index=1}

There are two main methods for drawing rectangles:

  • Draw the rectangle directly with the DrawRectangle method
  • Create a Rectangle object first, then draw it

27.2 Method 1 - DrawRectangle Directly

You can draw a rectangle directly by specifying the upper-left corner coordinates, width, and height. The basic syntax is:

myGraphics.DrawRectangle(myPen, X, Y, width, height)

Where:

  • myGraphics is the Graphics object
  • myPen is the Pen object
  • X, Y are the coordinates of the upper-left corner
  • width and height are the size of the rectangle

Example code:

Dim myPen As Pen
myPen = New Pen(Drawing.Color.Blue, 5)

Dim myGraphics As Graphics = Me.CreateGraphics()

myGraphics.DrawRectangle(myPen, 0, 0, 100, 50)

27.3 Method 2 - Create a Rectangle Object First

The second method is to create a Rectangle object first, and then draw it using the DrawRectangle method. The syntax is:

myGraphics.DrawRectangle(myPen, myRectangle)

To create the rectangle object:

Dim myRectangle As New Rectangle
myRectangle.X = 10
myRectangle.Y = 10
myRectangle.Width = 100
myRectangle.Height = 50

You can also create it in one line:

Dim myRectangle As New Rectangle(X, Y, width, height)

Then draw it like this:

myGraphics.DrawRectangle(myPen, myRectangle)

27.4 Customizing the Pen Line Style

So far, the shapes have been drawn using solid lines. In Visual Basic 2015, you can customize the line style of the Pen object to create dotted, dashed, or mixed line patterns. :contentReference[oaicite:2]{index=2}

For example, to create a dotted line:

myPen.DashStyle = Drawing.Drawing2D.DashStyle.Dot

Other useful dash styles include:

  • Dash
  • DashDot
  • DashDotDot
  • Solid

Example code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myPen As Pen
    myPen = New Pen(Drawing.Color.Blue, 5)

    Dim myGraphics As Graphics = Me.CreateGraphics()

    myPen.DashStyle = Drawing.Drawing2D.DashStyle.Dot
    myGraphics.DrawRectangle(myPen, 10, 10, 100, 50)
End Sub

This program draws a blue dotted rectangle.

Drawing rectangle with dotted line in Visual Basic 2015

Figure 27.1 – Rectangle with dotted line

Build on This Foundation

Continue to VB2026

After learning the basics of checkbox controls in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 →

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Next: Drawing Ellipse & Circle

Go to Lesson 28 →
🚀 Learn VB2026