Lesson 27: Drawing Rectangles in VB2019

Learn to create rectangles and customize line styles with GDI+

Key Takeaway

Visual Basic 2019 provides two efficient methods for drawing rectangles using GDI+, with options to customize line styles for creative designs.

Building on the graphics concepts from the previous lesson, we'll now explore how to draw rectangles in VB2019. Rectangles are fundamental shapes used in everything from UI elements to game graphics.

Direct Method

Draw rectangles by specifying coordinates directly

Rectangle Object

Create reusable rectangle objects for complex designs

Line Styles

Customize lines with dots, dashes and combinations

Creative Control

Full control over position, size and appearance

27.1 Drawing Rectangles in VB2019

In Visual Basic 2019, you can draw rectangles using two primary methods. Both approaches leverage the Graphics and Pen objects we learned about in Lesson 26.

Method 1: Direct Drawing

Draw a rectangle directly by specifying its upper-left corner coordinates, width, and height.

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

Where:

  • X, Y: Coordinates of the upper-left corner
  • width: Width of the rectangle
  • height: Height of the rectangle

Method 2: Rectangle Object

First create a Rectangle object, then draw it using the DrawRectangle method.

Dim myRectangle As New Rectangle(X, Y, width, height) myGraphics.DrawRectangle(myPen, myRectangle)

Benefits:

  • Reusable rectangle definitions
  • Easier manipulation of multiple rectangles
  • Better organization for complex designs

27.2 Drawing Rectangles: Code Examples

Direct Drawing Method

This example creates a blue rectangle with a 5-pixel border:

Form1.vb
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    ' Create the Graphics object
    Dim myGraphics As Graphics = Me.CreateGraphics
    
    ' Create a blue pen with 5px width
    Dim myPen As New Pen(Drawing.Color.Blue, 5)
    
    ' Draw rectangle at (20,30) with width=200, height=100
    myGraphics.DrawRectangle(myPen, 20, 30, 200, 100)
End Sub
Rectangle drawn with VB2019

Figure 27.1: Rectangle drawn using direct method

Rectangle Object Method

This approach creates a reusable Rectangle object before drawing:

Form1.vb
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    ' Create the Graphics object
    Dim myGraphics As Graphics = Me.CreateGraphics
    
    ' Create a red pen with 3px width
    Dim myPen As New Pen(Drawing.Color.Red, 3)
    
    ' Create a rectangle object
    Dim myRectangle As New Rectangle(50, 75, 150, 80)
    
    ' Draw the rectangle
    myGraphics.DrawRectangle(myPen, myRectangle)
End Sub

The Rectangle object approach is particularly useful when you need to:

  • Draw multiple rectangles with similar properties
  • Manipulate rectangle properties before drawing
  • Store rectangle definitions for later use

27.3 Customizing Line Styles

VB2019 allows you to customize the line style of your rectangles using various DashStyle options:

Line Style Options

DashStyle Description Example
Solid Continuous solid line (default) ──────
Dot Dotted line with small gaps ••••••
Dash Dashed line with medium gaps ------
DashDot Alternating dashes and dots -•-•-•
DashDotDot Dash followed by two dots -••-••

To apply a custom line style:

myPen.DashStyle = Drawing.Drawing2D.DashStyle.Dot

Example of drawing a rectangle with a dotted line:

Form1.vb
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    ' Create Graphics and Pen objects
    Dim myGraphics As Graphics = Me.CreateGraphics
    Dim myPen As New Pen(Drawing.Color.DarkMagenta, 5)
    
    ' Set dotted line style
    myPen.DashStyle = Drawing.Drawing2D.DashStyle.Dot
    
    ' Draw rectangle
    myGraphics.DrawRectangle(myPen, 30, 40, 180, 90)
End Sub
Dotted rectangle

Figure 27.2: Rectangle with dotted line style

Lesson Summary

In this lesson, you've learned how to create and customize rectangles in VB2019:

Direct Drawing

Draw rectangles by specifying coordinates, width and height

Rectangle Objects

Create reusable Rectangle objects for complex designs

Line Customization

Customize rectangle borders with various DashStyles

Practical Applications

Use rectangles for UI elements, game graphics, and more

With rectangle drawing mastered, you're ready to explore more complex shapes. In the next lesson, we'll learn to draw ellipses and circles.

Next Lesson

Learn to draw ellipses and circles in Lesson 28: Drawing Ellipses & Circles.

Related Resources

VB2019 Graphics Guide

Comprehensive guide to all graphics capabilities in VB2019

Explore Guide

Graphics Sample Code

Practical VB2019 code samples for graphics programming

View Examples

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon

VB2019 Kindle Edition

Digital version of Visual Basic 2019 Made Easy

Get on Kindle