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

Unit 4

This document discusses various concepts in VB.NET including functions, sub procedures, classes, objects, exception handling, and basic controls. It provides examples of how to define functions with and without parameters, pass parameters by value and reference, create classes and objects, handle exceptions, and use common controls like text boxes and buttons. Various properties and events of the text box and button controls are also outlined.

Uploaded by

directmart51
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)
36 views

Unit 4

This document discusses various concepts in VB.NET including functions, sub procedures, classes, objects, exception handling, and basic controls. It provides examples of how to define functions with and without parameters, pass parameters by value and reference, create classes and objects, handle exceptions, and use common controls like text boxes and buttons. Various properties and events of the text box and button controls are also outlined.

Uploaded by

directmart51
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/ 82

.

NET FRAMEWORK
UNIT-4
VB.Net - Functions
• A procedure is a group of statements that together perform a task
when called. After the procedure is executed, the control returns to
the statement calling the procedure. VB.Net has two types of
procedures −
Defining a Function
• The Function statement is used to declare the name, parameter and
the body of a function. The syntax for the Function statement is −
Function Example
Function Returning a Value
Example

OUTPUT
Create Function with parameter in VB.NET
This program shows the Function keyword. We provide an Area
Function: this Function receives a radius. It returns a Double that is the
area of a circle with that radius.

OUTPUT
Create Function without parameter in VB.NET
A Function must return a value—otherwise we must use a Subroutine.
But a Function does not need to receive a value—it can have an empty
parameter list.

OUTPUT
VB.Net - Sub Procedures
• Sub procedures are procedures that do not return any value.
Example of Sub Procedure

OUTPUT
Passing Parameters by Value
• This is the default mechanism for passing parameters to a method. In
this mechanism, when a method is called, a new storage location is
created for each value parameter. The values of the actual parameters
are copied into them. So, the changes made to the parameter inside
the method have no effect on the argument.
In VB.Net, you declare the reference parameters using the ByVal keyword.
The following example demonstrates the concept −

OUTPUT
Passing Parameters by Reference
• A reference parameter is a reference to a memory location of a
variable. When you pass parameters by reference, unlike value
parameters, a new storage location is not created for these
parameters. The reference parameters represent the same memory
location as the actual parameters that are supplied to the method.
In VB.Net, you declare the reference parameters using
the ByRef keyword. The following example demonstrates this −

OUTPUT
Difference between Sub Procedure & Function in VB.NET
Sub Procedures:
• A subprocedure is a group of VB.NET statements. It begins with a Sub
keyword and ends with End Sub keywords. A subprocedure is also
called a subroutine. It is used to execute a certain block of statements
consists the body of the procedure. It is called explicitly by its name
whenever it is required to perform a certain task. It can be called any
number of times. The subprocedure returns control to the calling
code after performing a task.
Syntax: Sub <subname> [(parameter list)]
Vb statements

End Sub
Example

OUTPUT
Function Procedures:
• A function procedure is a group of VB.NET statements. It begins with
a Function keyword and ends with an End Function keyword. It is
generally used to perform a task and return a value back to the calling
code. It may have multiple return points to the calling code. A part
from return stamens, End Function, or Exit function also returns
control to the calling procedure.
Syntax of Function Procedure:
Example

OUTPUT
Class and Object in VB.NET
• A class is a group of different data members or objects with the same
properties, processes, events of an object, and general relationships to
other member functions. Furthermore, we can say that it is like a template
or architect that tells what data and function will appear when it is
included in a class object. For example, it represents the method and
variable that will work on the object of the class.
Syantax:
[ Access_Specifier ] Class ClassName
' Data Members or Variable Declaration
' Methods Name
' Statement to be executed
End Class
Example of Class
In the below syntax, we have created a class with the name
'My_program' using the Class keyword.
Class Example

OUTPUT
Object
• An object is an instance of the class.

The Syntax for creating an object in VB.NET:


Example of Object
Create Class: Now create object:
Member Functions
A member function of a class is a function that has its definition or its
prototype within the class definition like any other variable. It operates
on any object of the class of which it is a member and has access to all
the members of a class for that object.
Member Functions Example
Constructors and Destructors
• A class constructor is a special member Sub of a class that is executed
whenever we create new objects of that class. A constructor has the
name New and it does not have any return type.
• Following program explains the concept of constructor −
Constructors Example

OUTPUT
Destructor
• A destructor is a special member Sub of a class that is executed
whenever an object of its class goes out of scope.
• A destructor has the name Finalize and it can neither return a value
nor can it take any parameters. Destructor can be very useful for
releasing resources before coming out of the program like closing
files, releasing memories, etc.
Destructor Example

OUTPUT
Properties in VB.NET
Properties: In VB.NET, a Property is similar to a Function. With a getter
and a setter, it controls access to a value. This value is called a backing
store.
With Get, a property returns a value.
With Set it stores a value.
We must have both Get and Set unless we specify ReadOnly or
WriteOnly on the property.
Properties Example

OUTPUT
VB.Net - Exception Handling
• An exception is a problem that arises during the execution of a
program. An exception is a response to an exceptional circumstance
that arises while a program is running, such as an attempt to divide
by zero.
• Exceptions provide a way to transfer control from one part of a
program to another. VB.Net exception handling is built upon four
keywords - Try, Catch, Finally and Throw.
Syntax of exception handler block
Example to Exception Handle

OUTPUT
Exception Classes in .Net Framework
• In the .Net Framework, exceptions are represented by classes. The
exception classes in .Net Framework are mainly directly or indirectly
derived from the System.Exception class. Some of the exception classes
derived from the System.Exception class are
the System.ApplicationException and System.SystemException classes.
• The System.ApplicationException class supports exceptions generated by
application programs. So the exceptions defined by the programmers
should derive from this class.
• The System.SystemException class is the base class for all predefined
system exception.
• The following table provides some of the predefined exception classes
derived from the Sytem.SystemException class −
Exception Classes in .Net Framework
Handling Exceptions

OUTPUT
Basic Controls
• VB.Net provides a huge variety of controls that help you to create rich
user interface. Functionalities of all these controls are defined in the
respective control classes. The control classes are defined in
the System.Windows.Forms namespace.

• The following table lists some of the commonly used controls −


Basic Controls List
Control Name Description
1. TextBox Control It represents a Web Text box control.
2. Button It represents a Web button control.
3. Label It represents a standard Web label.
4. Button It represents a Web button control.
5. CheckBox It represents a Web CheckBox.
6. RadioButton It enables the user to select a single option from a group of choices when
paired with other RadioButton controls.

7. ListBox It represents a Web control to display a list of items.


VB.Net - TextBox Control
• Text box controls allow entering text on a form at runtime. By default,
it takes a single line of text, however, you can make it accept multiple
texts and even add scroll bars to it.
• Let's create a text box by dragging a Text Box control from the Toolbox
and dropping it on the form.
The Properties of the TextBox Control
Sr.No. Property & Description
1 AutoCompleteMode
Gets or sets an option that controls how automatic completion works for the TextBox.
2 Font
Gets or sets the font of the text displayed by the control.
3 FontHeight
Gets or sets the height of the font of the control.
4 ForeColor
Gets or sets the foreground color of the control.
5 PasswordChar
Gets or sets the character used to mask characters of a password in a single-line TextBox control.
6 TextLength
Gets the length of text in the control.
Events of the TextBox Control
Sr.No. Event & Description
1 Click
Occurs when the control is clicked.

2 DoubleClick
Occurs when the control is double-clicked.
Textbox Example
VB.Net - Button Control
• The Button control represents a standard Windows button. It is
generally used to generate a Click event by providing a handler for the
Click event.
• Let's create a label by dragging a Button control from the Toolbox ad
dropping it on the form.
Properties of the Button Control
Sr. No. Property & Description

1. BackColor Gets or sets the background color of the control.

2. BackgroundImage Gets or sets the background image displayed in the control.

3. ForeColor Gets or sets the foreground color of the control.

4. Location Gets or sets the coordinates of the upper-left corner of the control relative
to the upper-left corner of its container.

5. Text Gets or sets the text associated with this control.


Events of the Button Control
Sr.No. Event & Description

1. Click Occurs when the control is clicked.

2. DoubleClick Occurs when the user double-clicks the Button control.

3. TextChanged Occurs when the Text property value changes.

4. Validated Occurs when the control is finished validating.


At this stage, the form looks like:
Clicking the first button, displays :
Clicking the second button displays:

Clicking the third button, exits the application.


VB.Net - Label Control
• The Label control represents a standard Windows label. It is generally
used to display some informative text on the GUI which is not
changed during runtime.
• Let's create a label by dragging a Label control from the Toolbox and
dropping it on the form.
Properties of the Label Control
Sr.No. Property & Description

1. BorderStyle Gets or sets the border style for the control.

2. Font Gets or sets the font of the text displayed by the control.

3. FontHeight Gets or sets the height of the font of the control.

4. ForeColor Gets or sets the foreground color of the control.

5. Text Gets or sets the text associated with this control.


Events of the Label Control
Sr.No. Event & Description

1. Click Occurs when the control is clicked.

2. DoubleClick Occurs when the control is double-clicked.

3. TextChanged Occurs when the Text property value changes.

4. GotFocus Occurs when the control receives focus.


Display
VB.Net - RadioButton Control
• The RadioButton control is used to provide a set of mutually exclusive options. The
user can select one radio button in a group. If you need to place more than one group
of radio buttons in the same form, you should place them in different container
controls like a GroupBox control.
• Let's create three radio buttons by dragging RadioButton controls from the Toolbox
and dropping on the form.
Properties of the RadioButton Control
Sr. No. Property & Description

1. Appearance Gets or sets a value determining the appearance of the radio button.

2. CheckAlign Gets or sets the location of the check box portion of the radio button.

3. Checked Gets or sets a value indicating whether the control is checked.

4. Text Gets or sets the caption for a radio button.


Example
Radio button Example
• When the above code is executed and run using Start button available
at the Microsoft Visual Studio tool bar, it will show the following
window −
VB.Net - ListBox Control
• The ListBox represents a Windows control to display a list of items to a user. A
user can select an item from the list. It allows the programmer to add items at
design time by using the properties window or at the runtime.
• Let's create a list box by dragging a ListBox control from the Toolbox and dropping
it on the form.
Properties of the ListBox Control
Sr. No. Property & Description

1. BorderStyle Gets or sets the type of border drawn around the list box.

2. ColumnWidth Gets of sets the width of columns in a multicolumn list box.

3. ItemHeight Gets or sets the height of an item in the list box.

4. Text Gets or searches for the text of the currently selected item in the list box.
Listbox Example
Listbox Example
• When the user chooses a destination, the text in the second label
changes −
VB.Net - ComboBox Control
• 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.
• Let's create a combo box by dragging a ComboBox control from the Toolbox and
dropping it on the form.
Properties of the ComboBox Control
Sr. No. Property & Description

1. AutoCompleteMode Gets or sets an option that controls how automatic completion works for
the ComboBox.

2. DataBindings Gets the data bindings for the control.

3. DropDownHeight Gets or sets the height in pixels of the drop-down portion of the ComboBox.

4. DropDownStyle Gets or sets a value specifying the style of the combo box.

5. Items Gets an object representing the collection of the items contained in this
ComboBox.

6. Text Gets or sets the text associated with this control.


Combo box Example
Combo box Example
• Click on various buttons to check the actions performed by each −
VB.Net - PictureBox Control
• The PictureBox control is used for displaying images on the form. The
Image property of the control allows you to set an image both at design
time or at run time.
• Let's create a picture box by dragging a PictureBox control from the
Toolbox and dropping it on the form.
Properties of the PictureBox Control
Sr. No. Property & Description

1. AllowDrop Specifies whether the picture box accepts data that a user drags on it.

2. Image Gets or sets the image that is displayed in the control.

3. ImageLocation Gets or sets the path or the URL for the image displayed in the control.

4. Text Gets or sets the text for the picture box.


Picture Box Example
Picture Box Example
• Clicking on the button results in −
VB.Net - CheckBox Control
• The CheckBox control allows the user to set true/false or yes/no type options. The
user can select or deselect it. When a check box is selected it has the value True, and
when it is cleared, it holds the value False.
• Let's create two check boxes by dragging CheckBox controls from the Toolbox and
dropping on the form.
Properties of the CheckBox Control
Sr. No. Property & Description

1. Appearance Gets or sets a value determining the appearance of the check box.

2. Checked Gets or sets a value indicating whether the check box is selected.

3. CheckState Gets or sets the state of a check box.

4. Text Gets or sets the caption of a check box.


CheckBox Example
CheckBox Example
• Checking all the boxes −
VB.Net - Event Handling
Handling Mouse Events
Example
When the above code is executed and run using Start button available at the
Microsoft Visual Studio tool bar, it will show the following window −
Handling Keyboard Events
Example
When the above code is executed and run using Start button available at the
Microsoft Visual Studio tool bar, it will show the following window −
******************** UNIT-4 END ************************

You might also like