Vbnet Form Helpcodes
Vbnet Form Helpcodes
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
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
3
Removing a selected item from the list
lstCountry.Items.Remove(lstCountry.SelectedItem.ToString)
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
4
ComboCountry.Items.Add(country)
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
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.
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:
9
10