VISUAL BASIC NET
& and +: used to join text and variables
MsgBox(" The names are " & name1 & " , " & name2 & " and " & name3)
MsgBox(" The names are " + txtName1.Text + " , " + txtName2.Text + " and " + txtName3.Text)
OUTPUT:
The names are Lovemore, Cridience and Shame
TEXTBOX
-Itis be used to input data
PublicClassForm1
PrivateSubbtnCALCULATE_Click(ByVal sender AsSystem.Object, ByVal e
AsSystem.EventArgs) HandlesbtnCALCULATE.Click
Dim No1, No2, Result AsInteger
No1 = Me.TxtFirstNo.Text
No2 = Me.TxtSecondNo.Text
Result = No1 + No2
Me.lblResult.Text = Result
EndSub
EndClass
1
LABEL
-Itis be used to display output
PublicClassForm1
PrivateSubbtnCALCULATE_Click(ByVal sender AsSystem.Object, ByVal e
AsSystem.EventArgs) HandlesbtnCALCULATE.Click
Dim No1, No2, Result AsInteger
No1 = Me.TxtFirstNo.Text
No2 = Me.TxtSecondNo.Text
Result = No1 + No2
Me.lblResult.Text = Result
EndSub
EndClass
BUTTON
-It is used to generate a Click event
- In the example above a user has to click the button written CALCULATE to display the result 35
on the label.
LISTBOX
-It is used to display a list of items
-A user can add, select, sort, deleteorclearitems from the list
-A list box can be created at design time from the properties window or run time
2
Adding items to the list box
Me.lstCountry.Items.Add("ZIMBABWE")
Or
//An item can be entered via a text box
Me.lstCountry.Items.Add= txtCountry.text
Or
//An item can be entered via an input box
Dim country As String
country = Microsoft.VisualBasic.InputBox("Enter a country and Click OK","Names Entry
Form", "Enter name here", 100, 200)
lstCountry.Items.Add(country)
NB: "Enter a country and Click OK" : Prompt message on the InputBox
"Names Entry Form" : Title of the InputBox
"Enter name here" : Default data in the text area of the InputBox
100, 200 : Size of the InputBox
Sorting items in a list
lstCountry.Sorted = True
3
Removing a selected item from the list
lstCountry.Items.Remove(lstCountry.SelectedItem.ToString)
An item can be removed using its index
lstCountry.Items.RemoveAt(1) //To remove the second item
To remove a selected item:
If lstCountry.SelectedIndex<> -1 Then
lstCountry.Items.RemoveAt(lstCountry.SelectedIndex)
End if
Displaying the number of items in the list on the labelist an
lblCount.Text = lstCountry.Items.Count
Clearing all items from the list
lstCountry.Items.Clear()
Get the index of the item in a list
lstCountry.Items.Index(“BOTSWANA”)
It will return a value of 1 as it is the second item. The first item is index (0)
To get the index of the selected item
lstCountry.SelectedIndex
COMBO BOX
-It is a combination of a text box and a list box
-It is used to display a drop list of items
-Items in the list are displayed one at a time and the user has to click the drop down list to see the
other items
The same program codes used on the List Box also apply on the Combo Box
Adding items to the list box
Me.ComboCountry.Items.Add("ZIMBABWE")
Or
//An item can be entered via a text box
Me.ComboCountry.Items.Add(txtCountry.text)
Or
//An item can be entered via an input box
Dim country As String
country = Microsoft.VisualBasic.InputBox("Enter a country and Click OK","Names Entry
Form", "Enter name here", 100, 200)
4
ComboCountry.Items.Add(country)
Sorting items in a combo box
ComboCountry.Sorted = True
Removing a selected item from the combo box
ComboCountry.Items.Remove(ComboCountry.SelectedItem.ToString)
An item can be removed using its index
ComboCountry.Items.RemoveAt(1) //To remove the second item
To remove a selected item:
If ComboCountry.SelectedIndex<> -1 Then
ComboCountry.Items.RemoveAt(ComboCountry.SelectedIndex)
End if
Displaying the number of items in the combo box on the label
lblCount.Text = ComboCountry.Items.Count
Clearing all items from the combo box
ComboCountry.Items.Clear()
NB: Code to add an item selected from the combo box onto a list box
If ComboBox.SelectedIndex> -1 Then
Dim sindex As Integer
sindex = ComboBox.SelectedIndex
Dim sitem As Object
sitem = ComboBox.SelectedItem
ListBox.Items.Add(sitem)
EndIf
CHECK BOX
-It allows the user to select or deselect one or more items by checking the checkbox or checkboxes
concerned.
-A selected check box has a True value, a deselected check box has a False value.
5
Example
PrivateSub Button1_Click(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs)
HandlesbtnCalculate.Click
Dim sum AsInteger = 0
DimsadzaAsInteger = 3
Dim rice AsInteger = 4
Dim beef AsInteger = 5
Dim chicken AsInteger = 6
IfMe.CheckSadza.Checked = TrueThen
sum = sum + sadza
EndIf
IfMe.CheckRice.Checked = TrueThen
sum += rice
EndIf
IfMe.CheckBeef.Checked = TrueThen
sum += beef
EndIf
IfMe.CheckChicken.Checked = TrueThen
sum += chicken
EndIf
Me.txtTotal.Text = sum.ToString("c")
EndSub
Example
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
6
TextBox1.Font = New Font(TextBox1.Font, TextBox1.Font.Style OrFontStyle.Italic)
Else
TextBox1.Font = New Font(TextBox1.Font, TextBox1.Font.Style And Not
FontStyle.Italic)
End If
End Sub
* The above program uses the CheckedChanged event to respond to the user selectionby checking a
particular checkbox; it is similar to the click event.
The statementTextBox1.Font = New Font(TextBox1.Font, TextBox1.Font.Style OrFontStyle.Italic)
will retain the original font type but change it to italic font style.
TextBox1.Font = New Font(TextBox1.Font, TextBox1.Font.Style And NotFontStyle.Italic)
will also retain the original font type but change it to regular font style.
RADIO BUTTON
-It operatesdifferently from the check boxes.
-The user can only choose one item only out of a number of choices.
-To select more than one item the radio buttons must be placed in different containers using a
GroupBox control
-Text, image or both can be displayed on the radio button.
Example
7
PrivateSubButtonFruit_Click(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs)
HandlesButtonFruit.Click
Dim Fruit AsString
IfMe.RadioButtonApple.Checked = TrueThen
Fruit = "Apple"
EndIf
IfMe.RadioButtonBanana.Checked = TrueThen
Fruit = "Banana"
EndIf
IfMe.RadioButtonMango.Checked = TrueThen
Fruit = "Mango"
EndIf
IfMe.RadioButtonOrange.Checked = TrueThen
Fruit = "Orange"
EndIf
Me.TextBoxFruit.Text = Fruit
EndSub
Activity
Write a program that allows a user to select a T-Shirt Color and a T-Shirt Size using radio buttons.
After clicking a button, the color and size of the selected T-Shirts must be displayed in textboxes as
shown below:
8
PICTURE BOX
-It is used to display images on the form.
Example
PrivateSubButtonImage_Click(ByVal sender AsSystem.Object, ByVal e
AsSystem.EventArgs) HandlesButtonImage.Click
Me.PictureBox.ClientSize = NewSize(300, 300)
PictureBox.SizeMode = PictureBoxSizeMode.StretchImage
EndSub
Design View:
Clicking the button results in the following:
9
10