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.
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.
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:
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
Figure 27.1: Rectangle drawn using direct method
Rectangle Object Method
This approach creates a reusable Rectangle object before drawing:
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:
Example of drawing a rectangle with a dotted line:
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
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
Visual Basic 2019 Made Easy
Master Visual Basic 2019 with this comprehensive guide that includes detailed coverage of graphics programming. Learn to create professional applications with custom graphics and visual elements.
Key Graphics Topics:
- Drawing shapes: rectangles, ellipses, polygons, and more
- Working with colors, gradients, and textures
- Creating animations with the Timer control
- Building custom UI elements with graphics
- Practical projects with full source code
Visual Basic Programming With Code Examples
This comprehensive guide includes numerous examples of graphics programming in both VB6 and VB.NET, helping you understand the evolution of graphics capabilities in Visual Basic.
Graphics Coverage:
- 20+ graphics programming examples
- Step-by-step drawing tutorials
- Custom control creation with graphics
- Animation techniques and examples