Lecture One For Windows Programming
Lecture One For Windows Programming
WINDOWS PROGRAMMING
SON OF MAN
CUSTOMIZING FORM 2
The first step in developing a Visual Basic 2015 application is to build a graphical user interface. When you
launch VB2015, you will be presented with a simple interface, a plain form. In order to develop a usable
program, you need to add some objects to the form from the toolbox, these objects are called controls.
After adding controls to the form, you can then write code for all the controls. The purpose of the code is for
the controls to respond to events triggered by the user and from a sub procedure. For example, some of the
user's actions are clicking the mouse and pressing a key. Therefore, Visual Basic 2015 is also an event-driven
programming language.
SON OF MAN
CUSTOMIZING THE PROPERTIES OF THE DEFAULT-FORM USING
THE PROPERTIES WINDOW 3
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 below
SON OF MAN
4
The properties window comprises an object drop-down list and a list of properties. In addition, its bottom
section shows a description of the selected property. The properties window displays all properties related
to an object on the VB2015 IDE, in this case, it is Form1. Besides that, it also displays the properties
corresponding attributes or values, as shown below.
In the properties windows, you can change the name of the object, the title of the object, the background
color, the foreground color, the size and more. You may change the properties by typing a value or by
selecting a value from a drop-down list. On the other hand, for the color setting, you just need to select a
color rectangle or from a color palette. Now customize the following properties for Form1:
SON OF MAN
Property Value 5
Name MyForm
Text My First VB2015 Project
BackColor 128, 255, 255
MaximizeBox False
Font Arial, 9.75pt
The value for Backcolor (background color) 128,255,255 is actually the RGB color code for Cyan. Instead of
typing the RGB value, you can indeed select a colour from the color drop-down list that comprises three tabs,
Custom, Web, and System. Clicking on the drop-down arrow will bring out a color palette or a list of color
rectangles where you can select a color, as shown the figures below:
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, the background changed to cyan color and the window cannot be 6
maximized.
SON OF MAN
Changing the Properties of the Default-Form at
Run-TimeDefault Form
7
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 object’s name followed by a dot or period:
ObjectName.property
For example, we can set the background color of the form to cyan using the following code
Me.BackColor=Color.Cyan
MSGBOX AND INPUTBOX 8
A function in Visual Basic 2015 is similar to a normal procedure but the main purpose of the function is to
accept a certain input and return a value which is passed on to the main program to finish the execution.
There are two types of functions in Visual Basic 2015, the built-in functions (or internal functions) and the
functions created by the programmers.
FunctionName (arguments)
The arguments are values that are passed on to the function. In this lesson, we are going to learn two very
basic but useful internal functions of Visual Basic 2015 , i.e. the MsgBox( ) and InputBox ( ) functions.
SON OF MAN
MSGBOX ( ) FUNCTION
9
The objective of MsgBox is to produce a pop-up message box and prompt the user to click on a command
button before he /she can continues. This format is as follows:
The first argument, Prompt, will display the message in the message box. The Style Value will determine
what type of command buttons appear on the message box, please refer to Table below for types of
command button displayed. The Title argument will display the title of the message board.
0 vbOkOnly Ok button
1 vbOkCancel Ok and Cancel buttons
Abort, Retry and Ignore
2 vbAbortRetryIgnore
buttons.
3 vbYesNoCancel Yes, No and Cancel buttons
4 vbYesNo Yes and No buttons
5 vbRetryCancel Retry and Cancel buttons
Style Values
Value Named Constant Button Clicked
1 vbOk Ok button 10
2 vbCancel Cancel button
Example 12.1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testmsg As Integer
testmsg = MsgBox("Click to test", 1, "Test message")
If testmsg = 1 Then
MessageBox.Show("You have clicked the OK button")
Else
MessageBox.Show("You have clicked the Cancel button")
End If
End Sub
To make the message box looks more sophisticated, you can add an icon besides the
message. There are four types of icons available in VB2015 as shown below
11
SON OF MAN
Example 12.2
12
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testMsg As Integer
testMsg = MsgBox("Click to Test", vbYesNoCancel + vbExclamation, "Test Message")
If testMsg = 6 Then
MessageBox.Show("You have clicked the yes button")
ElseIf testMsg = 7 Then
MessageBox.Show("You have clicked the NO button")
Else
MessageBox.Show("You have clicked the Cancel button")
End If
End Sub
default-text - The default text that appears in the input field where users can use it as his intended input or
he may change to the message he wish to enter.
x-position and y-position - the position or the coordinates of the input box.
THE INPUTBOX( ) FUNCTION 14
Example 12.3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim userMsg As String
userMsg = Microsoft.VisualBasic.InputBox("What is your message?", "Message Entry Form", "Enter
your messge here", 500, 700)
If userMsg <> "" Then
MessageBox.Show(userMsg)
Else
MessageBox.Show("No Message")
End If
End Sub