Visual Basic 2017 Lesson 27: Drawing Rectangles

[Lesson 26] << [Contents] >> [Lesson 28]

In the previous lesson , we have learned how to create Graphics and the Pen objects to draw straight lines in Visual Basic 2017 . Now we shall learn how to draw rectangles.

27.1 Drawing Rectangle

To draw a rectangle in Visual Basic 2017, there are two methods:

Method 1

The first  is to draw a rectangle directly using the DrawRectangle method by specifying its upper-left corner’s coordinate and its width and height. The method of the Graphics object to draw the rectangle is DrawRectangle .The syntax is:

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




*myGraphics is the variable name of the Graphics object and myPen is the variable name of the Pen object created by you. You can use any valid and meaningful variable names. X, Y is the coordinate of the upper left corner of the rectangle while width and height are the width and height of the rectangle.
The  code is as follows:

Dim myPen As Pen

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

Dim myGraphics As Graphics = Me.CreateGraphics

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

Method 2

The second method is to create a rectangle object first and then draw this rectangle using the DrawRectangle method. The syntax is as shown below:

myGraphics.DrawRectangle(myPen,myRectangle)

where myRectangle is the rectangle object created by you, the user.

The code to create a rectangtle object is as shown below:

Dim myRectangle As New Rectangle

myRect.X = 10

myRect.Y = 10

myRect.Width = 100

myRect.Height = 50

You can also create a rectangle object using a one-line code as follows:

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

and the code to draw the above rectange is

myGraphics.DrawRectangle(myPen, myRectangle)



27.2 Customizing Line Style of the Pen Object

The shape we draw so far are drawn with solid line. However, we can customize the line style of the Pen object so that we can draw dotted line, line consisting of dashes and more. For example, the syntax to draw with dotted line is shown below:

myPen.DashStyle=Drawing.Drawing2D.DashStyle.Dot

Where the last argument Dot specifies a particular line DashStyle value, a line that makes up of dots here. Other DashStyles values are Dash, DashDot, DashDotDot and Solid.The following code draws a rectangle with blue dotted line.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim 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

The output image is as  shown in Figure 27.1

vb2013_figure26.1

 Figure 27.1




If you change the DashStyle value to DashDotDot, you will get the following rectangle :

vb2013_figure26.2Figure 27.2

[Lesson 26] << [Contents] >> [Lesson 28]

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on Tumblr