Sei sulla pagina 1di 7

Visual Basic 2015 Lesson 27:

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

27.1 Creating Rectangle


To draw a rectangle on the default form in Visual Basic 2015, there are two
ways:

(i)The first is to draw a rectangle directly using


the DrawRectangle method by specifying its upper-left corners
coordinate and it width and height. You also need to create a Graphics and
a Pen object to handle the actual drawing. The method of the Graphics
object to draw the rectangle is DrawRectangle .The syntax is:

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

Where 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 self-explanatory,
i.e, 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)

(ii) The second way is to create a rectangle object first and then draw this
triangle 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, we can actually
customize the line style of the Pen object so that we have 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


Figure 27.1

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

Figure 27.2
Visual Basic 2015 Lesson 2:
Building the Interface1-
Customizing the Form
The first step in developing a Visual Basic 2015 application is to
build a graphical user interface. To build a graphical user interface, add
controls from the toolbox to the form and then customize their properties.
Note that the default form is also a control by itself and you can change
its properties first before adding additional controls. After adding controls
to the form, you then need to write code for all the controls so that they
can response to events triggered by the users actions such as clicking the
mouse. Therefore, Visual Basic 2015 is also an event-driven programming
language. We will learn more about the concept of event-driven
programming and coding in later lessons.

2.1 Customizing the Properties of the Default-Form


using the Properties Window

When you start a new Visual Basic 2015 project, the IDE will display the
default form along with the Solution Explorer window and the Properties
window on the far right, as shown in Figure 2.1.

Figure 2.1: Initial Visual Basic 2015 IDE


The properties window comprises an object drop-down list, a list of
properties and the bottom section that shows a description of the selected
property. As the only object on the IDE is the default form by the name of
Form1 , the properties window displays all properties related to Form1 and
their corresponding attributes or values, as shown in Figure 2.2.

You can change the name of the form, the title of the form, the
background colour, the foreground color , the size and more. Properties
can be changed by typing a value or select a value from a drop-down
list .For colour setting, you just need to select a colour rectangle or from a
colour palette. Now customize the following properties for Form1:

Figure 2.2: The Properties window

PROPERTY VALUE
Name MyForm
Text My First VB2015 Project
BackColor 128, 255, 255
MaximizeBox False
Font Arial, 9.75pt

* The value for Backcolor (background colour) 128,255,255 is actually the


RGB colour code for Cyan. Instead of typing the RGB value, you can
indeed select colour from the colour drop-down list that comprises three
tabs, Custom, Web and System. Clicking on the drop-down arrow will bring
out a colour palette or a list of colour rectangles where you can select a
colour, as shown the figures below:

Figure 2.3: Custom Figure 2.4: Web Figure 2.5: System


Palette Colours Colours

The runtime interface is shown in Figure 2.6. Notice that the title of the
form has been changed from Form1 to My First VB2015 Project,
background changed to cyan colour and the window cannot be
maximized.

Figure 2.6: The Runtime Interface


2.2 Changing the Properties of the Default-Form at Run-
Time

You can also change the properties of the form at run-time by writing the
relevant codes. The default form is an object and an instant of the form
can be denoted by the name Me. The property of the object can be
defined by specifying the objects name followed by a dot or period:

ObjectName.property

For example, we can set the background colour of the form to cyan using
the following code

Me.BackColor=Color.Cyan

To achieve the same interface as in Figure 2.6, type in the following code
by clicking the form to enter the code window:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

Me.Text = My First VB2015 Project

Me.BackColor = Color.Cyan

Me.MaximizeBox=False

Me.MinimizeBox = True

End Sub

End Class

Potrebbero piacerti anche