Lecture Two For Windows Programming
Lecture Two For Windows Programming
Lecture Two For Windows Programming
Let's create a label in the VB.NET Windows by using the following steps:
Step 1: We have to drag the Label control from the Toolbox and drop it on the Windows form, as
shown below.
VB.NET Label Control
2
Step 2: Once the Label is added to the form, we can set various properties to the Label by clicking on the
Label control.
Properties Description
AutoSize As the name defines, an AutoSize property of label control is used to set or get a value
if it is automatically resized to display all its contents.
Border Style It is used to set the style of the border in the Windows form.
PreferredWidth It is used to set or get the preferred width for the Label control.
Font It is used to get or set the font of the text displayed on a Windows form.
PreferredHeight It is used to set the height for the Label Control.
TextAlign It is used to set the alignment of text such as centre, bottom, top, left, or right.
ForeColor It is used to set the color of the text.
Text It is used to set the name of a label in the Windows Form.
ContextMenu It is used to get or sets the shortcut menu associated with the Label control.
VB.NET Label Control
3
Properties Description
SON OF MAN
CUSTOMIZING THE PROPERTIES OF THE DEFAULT-FORM USING
THE PROPERTIES WINDOW 6
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
7
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 8
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 9
maximized.
SON OF MAN
Changing the Properties of the Default-Form at
Run-TimeDefault Form
10
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 11
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
12
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 13
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
14
SON OF MAN
Example 12.2
15
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
myMessage is a variant data type but typically it is declared as string, which accept the message input by the
users. The arguments are explained as follows:
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 17
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