Unit III
Unit III
Handing Events
• To see the events associated with a form, open the Code Editor. From the Objects list on the
top left, choose (Base Class Events). The Procedures drop-down list on the top right will
then display all form events
• When the user click the event in the Procedures list, the event procedure framework code is
automatically created within the form code.
• Once an event procedure has been created, you can add code that is activated in response to
the event. This code is said to handle the event. It goes between the beginning and end of the
procedure that has been created.
• For example, when you choose the form Click event, the following empty event procedure is
created:
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Click
End Sub
• Form events fall into three general categories. These categories are form birth, user
interaction with a running form, and form death.
Handling Mouse Events
Mouse events occur with mouse movements in forms and controls. Following are the various mouse
events related with a Control class −
• MouseDown − it occurs when a mouse button is pressed
• MouseEnter − it occurs when the mouse pointer enters the control
• MouseHover − it occurs when the mouse pointer hovers over the control
• MouseLeave − it occurs when the mouse pointer leaves the control
• MouseMove − it occurs when the mouse pointer moves over the control
• MouseUp − it occurs when the mouse pointer is over the control and the mouse button is
released
• MouseWheel − it occurs when the mouse wheel moves and the control has focus
• The Toolbox window displays controls that you can add to Visual Studio projects. To
open Toolbox, choose View > Toolbox from the menu bar, or press Ctrl+Alt+X.
• We can drag and drop different controls onto the surface of the designer you are using, and
resize and position the controls.
PictureBox control is used to display the images on Windows Form. The PictureBox control
can be used to display bitmap graphic files in BMP, GIF, ICO, JPG, and PNG formats, as well as
Windows metafiles (WMF files).
Loading a Picture
• A PictureBox's Image property is used to control the graphics file that is displayed by the
PictureBox.
• To load a picture, select the Image property in the Properties window.
• Click the button marked with ellipses on the right side of the Properties window.
• The Open dialog will appear. Once you have selected a graphics file, it will appear in the
PictureBox control at both design time and runtime.
After you've added a PictureBox to a form, make sure to size and position it as you would like. The
properties are
✓ Dock,
✓ Anchor, and
✓ SizeMode
The size of the displayed image depends on how you set the PictureBox control's properties. For
example, if the PictureBox's Dock property is set to Full, the picture will occupy the entire client area
of the form at design time and runtime, as shown next.
• Setting the Anchor Property
By default, controls anchor in the top-left area of the form. We can use the Anchor property to set
a PictureBox (or any other control) to anchor elsewhere (for example, the bottom right).
PictureBox's SizeMode property is used to manipulate the graphics file in relationship to the
PictureBox. For example, suppose the PictureBox is bigger than the graphic and you want to stretch
the graphic to fill the PictureBox. Setting the SizeMode property of the PictureBox to StretchImage
will achieve this. We can set the SizeMode property in the Properties window or in code. To set it in
code, add this statement to an event handler or at initialization:
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
• The FromFile method of the Image class is used to assign a graphics file to the PictureBox's
Image property:
PictureBox1.Image = Image.FromFile(Path)
End Sub
PictureBox1.Image = Nothing
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Adding Checkbox controls
• The CheckBox control is a control that allows the user to select or deselect options from the
available options. When a checkbox is selected, a tick or checkmark will appear on the
Windows form.
• When a check box is selected it has the value True, and when it is cleared, it holds the value
False.
1 AutoCheck
The AutoCheck property is used to check whether the checked value or appearance of
control can be automatically changed when the user clicked on the CheckBox control.
2
CheckAlign
It is used to set the checkmark's alignment, such as horizontal or vertical on the checkbox.
3 Checked
The CheckState property is used to verify whether the checkbox status is checked in the
window form.
4
CheckState
Gets or sets the state of a check box.
5 Text
Gets or sets the caption of a check box.
6 ThreeState
The ThreeState property is used to check whether the control allows one to set three check
positions instead of two by setting values.
7
Appearance
The Appearance property is used to display the appearance of a checkbox control by
setting a value.
• The VB .NET CheckBox control can be used in a two-state or three-state mode, depending
on whether its ThreeState property is True or False.
• When ThreeState is False, the control's Checked property is used to determine the state of the
CheckBox. The Checked value can be either True (the CheckBox is checked or indeterminate)
or False (the CheckBox is not Checked).
• When ThreeState is True, the control's CheckState property is used to determine the control's
state.
• The CheckState value can be Checked (the CheckBox is checked), Unchecked (the CheckBox
is unchecked), or Indeterminate (the CheckBox is displayed with a dimmed appearance to
indicate that the option is not available).
1 OnCheckedChanged
Raises the CheckedChanged event.
2 OnCheckStateChanged
Raises the CheckStateChanged event.
3 OnClick
Raises the OnClick event.
CheckBox Events
Event Description
CheckedChanged The CheckedChanged event is found when the value of the checked
property is changed to CheckBox.
DoubleClick It occurs when the user performs a double click on the CheckBox control.
CheckStateChanged It occurs when the value of the CheckState property changes to the
CheckBox control.
Example
Fruits Billing
Collections are data structures that hold objects and give you ways to access, add, and remove objects
in the collection.
A For Next loop is used to repeatedly execute a sequence of code or a block of code until a given
condition is satisfied. A For loop is useful in such a case when we know how many times a block of
code has to be executed.
Syntax
Next [counter]
• start To end: The start and end are the two important parameters representing the initial
and final values of the variable_name. These parameters are helpful while the execution
begins, the initial value of the variable is set by the start. Before the completion of each
repetition, the variable's current value is compared with the end value. And if the value of
the variable is less than the end value, the execution continues until the variable's current
value is greater than the end value. And if the value is exceeded, the loop is terminated.
• Step Increment: A step parameter is used to determine by which the counter value of a
variable is increased or decreased after each iteration in a program. If the counter value is
not specified; It uses 1 as the default value.
• Exit For : Optional. Transfers control out of the For loop.
Example
Finding Factorial
An array is a linear data structure that is a collection of data elements of the same type stored on a
contiguous memory location. Each data item is called an element of the array. It is a fixed size of
sequentially arranged elements in computer memory with the first element being at index 0 and the
last element at index n - 1, where n represents the total number of elements in the array.
Array Initialization
We can also initialize the array elements while declaring the array.
For example,
Dim intData() As Integer = {12, 16, 20, 24, 28, 32}
Dim names() As String = {"Karthik", "Sandhya", "Shivangi", "Ashwitha", "Somnath"}
The elements in an array can be stored and accessed by using the index of the array. The following
program demonstrates this −
Example
Working with ComboBoxes, ListBoxes, and CheckedListBoxes
List Box
The ListBox control is used to display a list of items in Windows form. It allows the user to select
one or more items from the ListBox Control.
ListBox Properties
Properties Description
Name
AllowSelection It takes a value that defines whether the list box allows the user to select the
item from the list.
ColumnWidth It is used to get or set the width of the columns in a multicolumn Listbox.
SelectedIndex Gets or sets the zero-based index of the currently selected item in a list box.
SelectedIndices Gets a collection that contains the zero-based indexes of all currently selected
items in the list box.
SelectedItem Gets or sets the currently selected item in the list box.
SelectedItems Gets a collection containing the currently selected items in the list box.
Sorted Gets or sets a value indicating whether the items in the list box are sorted
alphabetically.
SelectionMode It is used to get or set the method that determines which items are selected in
the ListBox.
MultiColumn It allows multiple columns of the item to be displayed by setting the True value
in the Listbox.
Text Gets or searches for the text of the currently selected item in the list box.
ListBox Methods
Contains It is used to check whether the particular item exists in the ListBox or not.
Sort As the name suggests, a Sort() method is used to arrange or sort the elements
in the ListBox.
GetSelected The GetSelected method is used to validate whether the specified item is
selected.
1
Click
Occurs when a list box is selected.
2
SelectedIndexChanged
Occurs when the SelectedIndex property of a list box is changed.
• To demonstrate how to add items at design time, start a new project and insert a list box on
the form. Right-click on the list box to access the properties window. Next, click on collection
of the Item property, you will be presented with String Collection Editor whereby you can
enter the items one by one by typing the text and press the Enter key. After clicking on the
OK button, the items will be displayed in the list box
• In Visual Basic 2017, Items can also be added at runtime using the Add( ) method.
Syntax:
ListBox.Item.Add(“Text”)
For example, if you wish to add a new item to ListBox1 above, you can key-in the following
statement
• To delete 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 delete an item at runtime, you can use the Remove method in the following syntax:
ListBox1.Items.Remove(“text”)
ListBox1.Items.Remove(ListBox1.SelectedItem)
• 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"
ListBox1.Items.Clear()
Example
Combo Box
The ComboBox control is used to display a drop-down list of various items. It is a combination of a
text box in which the user enters an item and a drop-down list from which the user selects an item.
1
AllowSelection
Gets a value indicating whether the list enables selection of list items.
2
Items
Gets an object representing the collection of the items contained in this ComboBox.
3
MaxDropDownItems
Gets or sets the maximum number of items to be displayed in the drop-down part of the
combo box.
4
MaxLength
Gets or sets the maximum number of characters a user can enter in the editable area of
the combo box.
5
SelectedIndex
Gets or sets the index specifying the currently selected item.
6
SelectedItem
Gets or sets currently selected item in the ComboBox.
7
SelectedText
Gets or sets the text that is selected in the editable portion of a ComboBox.
8
Sorted
Gets or sets a value indicating whether the items in the combo box are sorted.
9
Text
Gets or sets the text associated with this control.
1 DropDown
Occurs when the drop-down portion of a combo box is displayed.
2 SelectedIndexChanged
Occurs when the SelectedIndex property of a ComboBox control has changed.
• In order to add items to the list at design time, you can also use the String Collection Editor.
You will have to type an item under the text property in order to display the default item at
runtime. The following is the runtime interface:
• Besides, We may add items using the Add() method. For example, if you wish to add an item
to Combo box 1
• To delete items at design time, simply open the String Collection Editor and delete the items
at line by line or all at once using the Delete key.
• To delete the items at runtime, you can use the Remove method
ComboBox1.Items.Remove(“Visual Basic 6”)
The item “Visual Basic 6" will be removed after running the program.
• 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"
ComboBox1.Items.Clear()
Example
Checked ListBox
The CheckedListBox is similar to Listbox except that it displays all items in the list with a checkbox
that allows users to check or uncheck single or multiple items.