Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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.
(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:
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:
myGraphics.DrawRectangle(myPen, myRectangle)
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.
End Sub
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.
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.
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:
PROPERTY VALUE
Name MyForm
Text My First VB2015 Project
BackColor 128, 255, 255
MaximizeBox False
Font Arial, 9.75pt
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.
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:
Me.BackColor = Color.Cyan
Me.MaximizeBox=False
Me.MinimizeBox = True
End Sub
End Class