You are on page 1of 6

Introduction to Visual Basic .

NET 1

Chapter I
The Visual Basic Integrated Development Environment
(Developing Desktop Applications)
1.0 Visual Basic .NET
Visual Basic .NET is the next generation of Visual Basic, but it is also a significant
departure from previous generations.
Experienced Visual Basic 6 developers will feel comfortable with Visual Basic .NET
code and will recognize most of its constructs.
However, Microsoft has made some changes to make Visual Basic .NET a better
language and an equal player in the .NET world.

1.1 .NET Framework


The .NET Framework consists of three parts: the Common Language Runtime, the
Framework classes, and ASP.NET, which are covered in the following sections.

1.1.1 .NET Architecture

.NET

.NET Framework .NET Server

ASP.NET
Updated ASP Engine
Web Forms Engine

Framework Classes
System.Math, System.lo, System.Data,Etc.

Common Language Runtime


Memory Management
Common Type System
Garbage Collection
ASP.NET
Active Server Pages were introduced, the language supported for server side scripting
was VBScript, not VB. (Technically, other languages could be used for server side scripting,
but VBScript has been the most commonly used.)
Framework Classes
In VB.NET, the compiler adopts this model. Many features that were formerly in
Visual Basic directly are now implemented through Framework classes.
Introduction to Visual Basic .NET 2

For example, if you want to take a square root, instead of using the VB operator, you use a
method in the System.Math class. This approach makes the language much more lightweight
and scalable.

Common Language Runtime


CLR provides the interface between the code and the operating system, providing
such features as Memory Management, a Common Type System, and Garbage Collection.

1.2 Creating a Windows Application


To create a GUI application in Visual Studio .NET:
1. Select File  New  Project. The New Project dialog box appears, as shown in figure.

2. Select Visual Basic Projects in the Project Types pane on the left side of the dialog box.
3. Select Windows Application in the Templates pane on the right side of the dialog box.
4. Enter a name in the Name text box.
5. Click OK button.
Fig: Windows Form Designer

Solution
Explorer

Toolbox

Properties
Form Windows
Design

1.3 Code for a Blank Form


Introduction to Visual Basic .NET 3

Public Class Form1


Inherits System.Windows.Forms.Form

+ Windows Form Designer generated code


|
End Class

1.4 An Example for VB.NET Program

1. Create a Window Application project called "HelloProgram".


2. Add a button called "btnHello" to the form.
3. Add a Textbox control to the form and set the Name "txtHello".
4. Double click the button control to create its Click event handler.

Private Sub btnHello_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnHello.Click
txtHello.Text = "Hello ! Introduction to Visual Basic .NET "
MsgBox("Hello! VB.Net Program", MsgBoxStyle.Information+
MsgBoxStyle.OKonly, "Message")
End Sub

Examples:
1.1 Date and Time Program

Create a Window Application project called "DateTimeProgram".


Add a button called "btnDateTime" to the form.
Add a Label control to the form and set the Name "lblDateTime".
Double click the button control to create its Click event handler.

1.2 List box Control


Introduction to Visual Basic .NET 4

1. Create a Window Application project named "AddListProgram".


2. Add a button named "btnAdd" to the form.
3. Add a textbox control named "txtName" and a List box control named "ListName".
4. Double click the button control to write codes for program.
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
lstName.Items.Add(txtName.Text)
End Sub

Private Sub frmList_Load(ByVal sender As Object, ByVal e As


System.EventArgs) Handles MyBase.Load
txtName.TabIndex = 0
End Sub

1.3 Loan Program

Create the following form with two buttons and four textbox controls.

Write the following code for program.

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnCalculate.Click
Dim Depo, IR, YR, Total As Double
Introduction to Visual Basic .NET 5

Depo = Val(txtDeposit.Text)
IR = Val(txtInterest.Text) / 100
YR = Val(txtYear.Text)
Total = Depo * (1 + YR * IR)
txtTotal.Text = Format(Total, "fixed")
End Sub

1.4 Clock Program

Create the form shown with a label, 2 buttons and a timer. Write the following code.

Private Sub frmClock_Load(ByVal sender As Object, ByVal e As


System.EventArgs) Handles MyBase.Load
lblTime.Text = Format(TimeOfDay(), "Long Time")
btnResume.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
lblTime.Text = Format(TimeOfDay(), "Long Time")
End Sub
Private Sub btnResume_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnResume.Click
Timer1.Enabled = True
btnResume.Enabled = False
btnPause.Enabled = True
End Sub
Private Sub btnPause_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnPause.Click
Timer1.Enabled = False
btnResume.Enabled = True
btnPause.Enabled = False
End Sub

1.5 Combo Box Program


Create the form design shown in the figure with a combo box and a Textbox control.

To enter items in the Combo box ;


Introduction to Visual Basic .NET 6

Choose Items and click browse button in the properties. Type the strings or items in the
String Collection Editor.

Write the following code.


Private Sub cboUniv_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles cboUniv.KeyPress

txtUniv.Text = cboUniv.Text

End Sub
Note:
To set as Startup Project;
Project Menu  ComboProgram Properties  Choose Form Name to set as Startup in the
Startup object combo box.
Click Apply button and OK button.

You might also like