Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views18 pages

3-Chap 3 FAC PROGRA

Uploaded by

tabipaul95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views18 pages

3-Chap 3 FAC PROGRA

Uploaded by

tabipaul95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

CHAPTER I : SYNTAX OF VB.

NET

I- FORM AND CONTROL ELEMENTS


In this section, we shall learn how to write codes for some common controls. Some of the
commonly used controls are label, textbox, button, list box and combo box. However, in this
chapter, we shall only deal with textbox , label, list box and combo box. We shall deal with
the other controls later chapters.

I.1 TextBox
A Textbox is the standard control for accepting inputs from the user as well as to display the
output. It can handle string and numeric data but not images or pictures. String in a textbox
can be converted to a numeric data by using the function Val(text). The following
example illustrates a simple program that processes the input from the user.

Example 4.1 Adding two numbers in two text boxes


In this program, you add two text boxes and a button on the form. The two text boxes are
for accepting inputs from the user. Besides that, we can also program a button to calculate
the sum of the two numbers using the plus operator. The value in a textbox is stored using
the syntax TextBox1.Text , where Text is one of the properties of textbox.
The following code will add the value in TextBox1 and the value in TextBox2 and displays
the sum in a message box. The runtime interface is illustrated in Figure 4.1. Private Sub
Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
MsgBox("The sum is" & Val(TextBox1.Text) + Val(TextBox2.Text))
End Sub

Course of Level one LA PERLE BY M TAGA


Figure 4.1
After clicking the Add button, you will obtain the answer in a message box, as shows in
Figure 4.2

. Figure 4.2
I.2 Label
The Label control can be used for multiple purposes like providing instructions and guides
to the users, displaying outputs and more. It is different from the textbox because it is read
only , which means the user cannot change or edit its content at runtime. Using the syntax
Label.Text, it can display string as well as numeric data . You can change its text property
in the properties window or at runtime by writing an appropriate code.

Example 4.2 Displaying output on a Label


Based on Example 4.1, we add two labels, one is for displaying the text Sum= and the other
label is to display the answer of the Sum. For the first label, change the text property of the
label by typing Sum= over the default text Label1. Further, change its font to bold and the
font size to 10. For the second label, delete the default text Label2 and change its font to
bold and the font size to 10. Besides that, change its background color to white.
In this program, instead of showing the sum in a message box, we display the sum on a
label.

Course of Level one LA PERLE BY M TAGA


The Code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Label2.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
End Sub
*The function Val is to convert text to numeric value. Without using Val, you will see that
two numbers are joined instead of adding them.
The output is as shown in Figure 4.3

Figure 4.3
I.3 LIST BOX
The function of the list box is to present a list of items where the user can click and select
the items from the list. Items can be added by the programmer at design time or at runtime
using a code. We can also write code to allow the user to add items to the list box or
remove the items from the list box.
I.3.1 Adding Items to the List Box
A) Adding items using the String Collection Editor
To demonstrate how to add items at design time, start a new project and insert a list box on
the form then right-click on the list box to access the properties window. Next, click on
collection of the Item property to launch the String Collection Editor whereby you can enter
the items one by one by typing the text and press the Enter key, as shown in Figure 4.4.
After clicking on the OK button, the items will be displayed in the text box, as shown in
Figure 4.5

Course of Level one LA PERLE BY M TAGA


b) adding items using the add() method
items can also be added at runtime using the add() method. before we proceed further, we
should know that visual basic 2017 is an object-oriented programming language.
therefore, visual basic 2017 comprises objects. all objects have methods and properties,
and they can be differentiated and connected by hierarchy. for a list box, an item is an
object subordinated to the object listbox . the item object comprises a method call add()
that is used to add items to the list box. to add an item to a list box, you can use the
following syntax: listbox.item.add("text")

Example 4.3 Adding an Item to a List Box


In this example, the item “Nokia” will be added to the end of the list, as shown in Figure 4.6

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles


Button1.Click
ListBox1.Items.Add("Nokia")
end Sub

Course of Level one LA PERLE BY M TAGA


Example 4.4 Adding items to a List Box via an input box
In this example, you can allow the user to add items via a popup input box. First, we create
a variable myitem and then assign a value to myitem via the InputBox function that store
the input from the user. We then use the Add() method to add the user’s item into the list
box. The code is as follows:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim myitem 'declare the variable myitem
myitem = InputBox("Enter your Item")
ListBox1.Items.Add(myitem)
End Sub
The runtime interface is as shown in Figure 4.7

Course of Level one LA PERLE BY M TAGA


Figure 4.7
After typing the item ‘Xiaomi” in the input box, the item will be added to the list box, as
shown in Figure 4.8.

Figure 4.8

I.3.2 Removing Items from a List Box

To remove items at design time, simply open the String Collection Editor and delete the
items line by line or all at once using the Delete key. To remove the items at runtime, you
can use the Remove method, as illustrated in the following Example 4.5.
Example 4.6 Removing an item from a list box
In this example, add a button and label it “Remove Items”. Click on this button and enter
the following code
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
ListBox1.Items.Remove("Ipad")
End Sub
The item “Ipad” will be removed after running the program.
Example 4.7 Deleting an item from a list box via an input box
You can also allow the user to choose an item to delete via an Inputbox. To add this
capability, insert a button at design time and change its text to Delete Item. Click on the
button and enter the following statements in the code window:

Course of Level one LA PERLE BY M TAGA


Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles
BtnDelete.Click
Dim myitem
myitem = InputBox("Enter your Item for Deletion")
MyListBox.Items.Remove(myitem)
End Sub
The runtime interface is as shown in Figure 4.10. After entering the item Samsung in the
input box and press OK, the item Samsung will be deleted from the list box.

To remove a selected item from the list box, using the following syntax:
Listbox1.Items.Remove(ListBox1.SelectedItem)

Example 4.8 Removing a selected item from a list box


Private Sub BtnDelSel_Click(sender As Object, e As EventArgs) Handles
BtnDelSel.Click
MyListBox.Items.Remove(MyListBox.SelectedItem)
End Sub
When the user run the program and select an item to delete, the item will be deleted.
To remove multiple selected items from the list box, you need to use the If...End If structure
together with the For…Next loop. Besides that, you also must ensure that the list box allows
multiple selection. To enable multiple selection, set the selection mode to MultipleSimple
in the list box properties windows. The code is as shown in Example 4.7

Example 4.1 0 Removing all items in a list box using the Clear method
In this example, add a button and label it “Clear the List”

Course of Level one LA PERLE BY M TAGA


Private Sub BtnClr_Click(sender As Object, e As EventArgs) Handles
BtnClr.Click
MyListBox.Items.Clear()
End Sub
When you run the program and click the “Clear the List” button, all the items will be cleared.
The design interface for remove the items from the list box is as shown in Figure 4.11

Figure 4.11

I.4 ComboBox
The function of the combo box is also to present a list of items where the user can click and
select the items from the list. However, the combo box only displays one item at runtime.
The user needs to click on the handle (small arrowhead) on the right of the combo box to
see all the items in a drop-down list.
I.4.1 Adding Items to a combo box
To add items to the combo box at design time, use the String Collection Editor as shown in
Figure 4.12. Besides that, if you want to display an item as the default text in the combo box
when you run the program, enter the name of the item by replacing the text property of the
combo box.

Course of Level one LA PERLE BY M TAGA


Figure 4.12
After clicking the handle of the right side of the combo box, the user will be able to view all
the items, as shown in Figure 4.13

Figure 4.13

Course of Level one LA PERLE BY M TAGA


Besides that, you may add items using the Add() method. For example, if you wish to add
an item to ComboBox1, key in the following statement. The output is as shown in Figure
4.14
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click ComboBox1.Items.Add("Nokia")
End Sub

Figure 4.14
You can also allow the user to add items via an input box, as follows:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim myitem
myitem = InputBox("Enter your Item")
ComboBox1.Items.Add(myitem)
End Sub
The runtime interface is shown in Figure 4.15

Course of Level one LA PERLE BY M TAGA


Figure 4.15
After you type the item ‘Xiaomi’ and click Ok, you can see that the item has been added to
the combo box, as shown in Figure 4.16.

I.4.2 Removing Items from a Combo box


To remove items from the combo box at design stage, simply open the String Collection
Editor and delete the items line by line or all at once using the Delete key.
To remove the items at runtime, use the Remove method, as illustrated in the following
example. In this example, add a second button and label it “Remove Items”. Click on this
button and enter the following code:

Course of Level one LA PERLE BY M TAGA


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
ComboBox1.Items.Remove("Ipad")
End Sub
The item “Ipad” will be removed after running the program. You can also let the user select
a specific item to delete, the code is as follows:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles


Button1.Click
MyCombo.Items.Remove(MyCombo.SelectedItem)
End Sub
To clear all the items at once, use the clear method, as illustrated in the following example.
In this example, add a button and label it “Clear Items”

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles


Button2.Click
ComboBox1.Items.Clear()
End Sub

Table 2-1 Fundamental Object Types and Their Uses

Course of Level one LA PERLE BY M TAGA


Figure 2.2 Hex Color Codes

Tips From The Pros


Creating a Main Menu
The development of a menu system is quite easy, provides one of the most visually
impressive functioning parts of a system, and actually demands the least in terms of
programming
competency. To develop a menu, follow these steps:
1. Make the opening application’s form a Main Menu form, which is a form that contains
buttons used to display other forms.
2. Initially, add a single new form to the project, which ultimately will be replaced by an
operational form. (Later on, additional new forms will be added, one for each operational
form required by the project.)
3. Provide the new form with a button having a caption such as Return to Main Menu

Course of Level one LA PERLE BY M TAGA


form. This button is referred to as the Return button.
4. Attach three lines of code to the new form’s Return button’s Click event.
These three lines of code appear as:
Dim frmMainRef As New frmMain()
Me.Hide ’ this is a valid statement
frmMainRef.Show ’ replace frmMain with any valid form name
II- CONTROL STRUCTURES
II.1Branching Statements
Visual Basic .NET supports a number of branching statements that interrupt the sequential
flow of
program execution and instead allow it to jump from one portion of a program to another.
These can
be either conditional statements (such as If or Select Case) or unconditional (such as Call
and Exit).
II.1.1 Exit
The Exit statement causes execution to exit the block in which the Exit statement appears. It is
generally used to prematurely break out of a loop or procedure when some unusual condition
occurs.
The Exit statement should be avoided when possible because it undermines the structure of
the
block in which it appears. For example, the exit conditions of a For loop should be
immediately
apparent simply by looking at the For statement. It should not be necessary to read through
the entire
loop to determine if there are additional circumstances under which the loop might exit. If a
given For
loop truly needs an Exit statement, investigate whether a different loop construct would be
better
suited to the task. If a given procedure truly needs an Exit statement, investigate whether the
procedure is factored appropriately.
The Exit statement has a different form for each type of block in which it can be used, as
listed here:
Exit Do
Exits a Do loop. Execution continues with the first statement following the Loop statement.
Exit For
Exits a For loop. Execution continues with the first statement following the Next statement.
Exit Function
Exits a function. Execution continues with the first statement following the statement that
called the function.
Exit Property
Exits a property get or property set procedure. Execution continues with the first statement
following the statement that invoked the property get or property set procedure.
Exit Sub

Course of Level one LA PERLE BY M TAGA


Exits a subroutine. Execution continues with the first statement following the statement that
called the subroutine.
Exit Try
Exits the Try clause of a Try block. If the Try block has a Finally clause, execution
continues with the first statement in the Finally clause. If the Try block does not have a
Finally clause, execution continues with the first statement following the Try block.
II.1.2 Goto
The Goto statement transfers execution to the first statement following the specified label. For
example:
' ...
Goto MyLabel
' ...
MyLabel:
' ...
The label must be in the same procedure as the Goto statement.
The Goto statement is generally avoided in structured programming because it often leads to
code
that is difficult to read and debug.
II.1.3 If
The If statement controls whether a block of code is executed based on some condition. The
simplest form of the If statement is:
If expression Then
statements
End If
expression is any expression that can be interpreted as a Boolean value. If expression is True,
the statements within the If block are executed. If expression is False, those statements are
skipped.
To provide an alternative set of statements to execute when expression is False, add an Else
clause, as shown here:
If expression Then
statements
Else
statements
End If
If expression is True, only the statements in the If clause are executed. If expression is False,
only the statements in the Else clause are executed.
Finally, a sequence of expressions can be evaluated by including one or more ElseIf clauses,
as
shown here:
If expression Then
statements
ElseIf expression Then
statements
ElseIf expression Then

Course of Level one LA PERLE BY M TAGA


statements
Else
statements
End If
II.1.4 Return
The Return statements exits a function and provides a return value to the caller of the
function.
Execution continues with the first statement following the statement that called the function.
Here is an
example:
Public Shared Function MyFactorial(ByVal value As Integer) As Integer
Dim retval As Integer = 1
Dim i As Integer
For i = 2 To value
retval *= i
Next
Return retval
End Function
II.1.5 SELECT CASE
The Select Case statement chooses a block of statements to execute based on some value. For
example:
The Select Case statement chooses a block of statements to execute based on some value. For
example:
Select Case strColor
Case "red"
' ...
Case "green"
' ...
Case "blue"
' ...
Case "yellow"
' ...
Case Else
' ...
End Select
II.2 ITERATION STATEMENTS
Iteration statements, also known as looping statements, allow a group of statements to be
executed more than once. The group of statements is known as the body of the loop. Three
statements fall under this category in Visual Basic .NET: Do, For, and For Each.

Course of Level one LA PERLE BY M TAGA


II.2.1 Do
The Do loop executes a block of statements either until a condition becomes true or while a
condition remains true. The condition can be tested at the beginning or at the end of each
iteration. If the test is performed at the end of each iteration, the block of statements is
guaranteed to execute at least once.
The Do loop can also be written without any conditions, in which case it executes repeatedly
until and unless an Exit Do statement is executed within the body of the loop. Here are some
examples of Do loops:
Do While i < 10
' ...
Loop
Do Until i >= 10
' ...
Loop

Loop
II.2.2 FOR
The For loop executes a block of statements a specified number of times. The number of
iterations is
controlled by a loop variable, which is initialized to a certain value by the For statement, then
is
incremented for each iteration of the loop. The statements in the body of the loop are
repeatedly
executed until the loop variable exceeds a given upper bound.
The syntax of the For loop is:
For variable = expression To expression [ Step expression ]
statements
Next [ variable_list ]
The loop variable can be of any numeric type. The variable is set equal to the value of the first
expression before entering the first iteration of the loop body. Prior to executing each iteration
of the loop, the loop variable is compared with the value of the second expression. If the value
of the loop variable is greater than the expression (or less than the expression if the step
expression is negative), the loop exits and execution continues with the first statement
following the Next statement. The step expression is a numeric value that is added to the loop
variable between loop iterations. If
the Step clause is omitted, the step expression is taken to be 1.
The Next statement marks the end of the loop body. The Next keyword can either appear by
itself in the statement or be followed by the name of the loop variable. If For statements are
nested, a single Next statement can terminate the bodies of multiple loops. For example:

Course of Level one LA PERLE BY M TAGA


For i = 1 To 10
For j = 1 To 10
For k = 1 To 10
' ...
Next k, j, I

This code is equivalent to the following:


For i = 1 To 10
For j = 1 To 10
For k = 1 To 10
' ...
Next
Next
Next
I recommend the latter style, since it is considered more structured to terminate each block explicitly.

Course of Level one LA PERLE BY M TAGA

You might also like