0% found this document useful (0 votes)
14 views

Unit III

Uploaded by

ABILESH.S 12-C1
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)
14 views

Unit III

Uploaded by

ABILESH.S 12-C1
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/ 14

Unit III

Event Driven Programming


• Event-driven programming is a programming paradigm in which the flow of program
execution is determined by events - for example a user action such as a mouse click, key press,
or a message from the operating system or another program.
• In Visual Basic, when the user action occurs for some events, then the particular then the
particular black of codes that t be executed for that event is performed
• Events can be fired in a number of ways, including the following:
✓ By an action taken by a user, such as clicking an object
✓ By a call to the event procedure in code
✓ By triggering a declared event procedure using the RaiseEvent statement
✓ By the operating system

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

Handling Keyboard Events


Following are the various keyboard events related with a Control class −
• KeyDown − occurs when a key is pressed down and the control has focus
• KeyPress − occurs when a key is pressed and the control has focus
• KeyUp − occurs when a key is released while the control has focus
Working with Windows Forms Controls

Working with Toolbox

• 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.

Customizing the Toolbox


Using the Toolbox's context menus, you can completely customize the appearance of Toolbox tabs
and their items. You can rearrange the items in a Toolbox tab or add custom tabs and items by using
the following commands on the right-click context menu:

• Rename Item - Renames the selected item.


• List View - Shows the controls in a vertical list. If unchecked, the controls appear
horizontally.
• Show All - Shows all possible controls (not just the ones that apply to the current designer).
• Choose Items - Opens the Choose Toolbox Items dialog box so that you can specify the
items that appear in the Toolbox. You can show or hide an item by selecting or clearing its
check box.
• Sort Items Alphabetically - Sorts the items by name.
• Reset Toolbar - Restores the default Toolbox settings and items.
• Add Tab - Adds a new Toolbox tab.
• Move Up - Moves the selected item up.
• Move Down - Moves the selected item down.

Adding PictureBox 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.

Sizing and Positioning Pictures

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

• Setting the Dock Property

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).

• Setting the SizeMode Property

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

Changing PictureBox Properties in Code

• The FromFile method of the Image class is used to assign a graphics file to the PictureBox's
Image property:

Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs)


Handles btnLoad.Click

Dim Path As String = "C:\WINDOWS\Soap Bubbles.bmp"

PictureBox1.Image = Image.FromFile(Path)

End Sub

• We also may want to remove a graphics file from a PictureBox at runtime.

PictureBox1.Image = Nothing

• We can set the SizeMode property in the Properties window or in code.

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.

Properties of the CheckBox Control

Sr.No. Property & Description

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.

Determining the CheckBox State

• 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).

Methods of the CheckBox Control

S.No. Method Name & Description

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.

AppearanceChanged It occurs when the property of the Appearance is changed to the


CheckBox control.

Example

Fruits Billing

Working with Groups of Controls

Collections are data structures that hold objects and give you ways to access, add, and remove objects
in the collection.

Using For..Next Statement

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

For counter = start To end [Step Increment]


statements
[Exit For]
statements

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

Using Arrays in VB .NET

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.

Creating Arrays in VB.Net


To declare an array in VB.Net, you use the Dim statement. For example,
Dim a(30) as integer ' an array of 31 elements
Dim name(20) As String ' an array of 21 strings
Dim twoDarray(10, 20) As Integer 'a two dimensional array of integers

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

ComboBoxes, ListBoxes, and—new to VB .NET—CheckedListBoxes are user interface controls for


dealing with lists of items.

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.

Items Gets the items of the list box..

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

Method Name Description

Add() The Add() method is used to add items to an item collection.

Remove It is used to remove an item from an item collection. However, we can


remove items using the item name.
Clear It is used to remove all items from the item collection at the same time.

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.

Events of the ListBox Control

Sr.No. Event & Description

1
Click
Occurs when a list box is selected.

2
SelectedIndexChanged
Occurs when the SelectedIndex property of a list box is changed.

Adding Items to a List Box

Items can be added at design time or at runtime.

• 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

ListBox1.Items.Add(“Microsoft Visual Studio 2017”)

Remving Items from a List Box

• 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”)

It is illustrated in the following example:

ListBox1.Items.Remove(“Visual Basic 6”)

• Here's the code for removing a selected item:

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.

Properties of the ComboBox Control

Sr.No. Property & Description

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.

Events of the ComboBox Control

Sr.No. Event & Description

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.

Adding Items to a Combo Box

• 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

ComboBox1.Items.Add(“Visual Studio 2017")

Removing Items from a Combo Box

• 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.

You might also like