GUI Application Development using VB .Net (22034) (1)
GUI Application Development using VB .Net (22034) (1)
Practical No: 03
Module Module1
Dim i, j, r As Integer
Sub Main()
Console.WriteLine("Enter First Value=")
i = Console.ReadLine()
Console.WriteLine("Enter Second
Value=") j = Console.ReadLine()
Console.ReadLine()
MsgBox("Addition =" & (i + j))
MsgBox("Substraction =" & (i - j))
MsgBox("Multiplication =" & (i * j))
MsgBox("Division =" & (i / j))
End Sub
End Module
Results (Output of the Program)
Enter First Value=
10
Enter Second Value=
5
---------------------------
MessageBoxPractical3
---------------------------
Addition =15
---------------------------
OK
---------------------------
It has prototype –
DialogResult Show(String text, String caption, MessageBoxButtons buttons,
MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
This method is static and doesn’t required and object reference. The parameters and return
values are explained below –
Parameters
1. text As String - The text to display in the message box.
2. caption As String - The text to display in the title bar of the message box.
3. buttons As MessageBoxButtons - One of the MessageBoxButtons values that
specifies which buttons to display in the message box.
4. icon As MessageBoxIcon - One of the MessageBoxIcon values that specifies which
icon to display in the message box.
5. defaultButton As MessageBoxDefaultButton - One of the
MessageBoxDefaultButton values that specifies the default button for the message
box.
Returns
DialogResult - One of the DialogResult values.
XIV. Exercise
1. Implement the program to generate result of any arithmetic operation using
MessageBox().
Addition=60
End Sub
End Class
Output:
Inputbox_Msgbox
Addition = 68
OK
Practical No: 04
End If
Else
If (b > c) Then
2. Implement the program using if-else statement to find the number is even or odd –
Module Module1
Dim i As Integer
Sub Main()
Console.WriteLine("Enter Number")
i = Console.ReadLine()
If ((i Mod 2) = 0) Then
Console.WriteLine("Number is Even")
Else
Console.WriteLine("Number is Odd")
End If
Console.ReadLine()
End Sub
End Module
XIV. Exercise
1. Write the program using if-else statement for following output.
Result Percentage Criteria
Fail perc < 40
Second Class perc > 40 AND perc < 60
First Class perc > 60 AND perc < 75
Distinction perc > 75
Module Module1
Sub Main()
Dim perc As Integer
Console.Write("Enter Percentage:")
perc = Val(Console.ReadLine())
End Module
Sub Main()
Dim i As Integer
i=4
Dim a As Double
a = -1.0
If (i > 0) Then
If (a > 0) Then
Console.WriteLine("Here I am !!!!")
Else
Console.WriteLine("No here I am ??")
Console.WriteLine("Actually here I am ??")
End If
End If
Console.ReadKey()
End Sub
End Module
Practical No: 05
Module Module1
Sub Main()
Dim grade As String
Console.WriteLine("Enter Your grade")
grade = Console.ReadLine()
Select Case grade
Case "A"
Console.WriteLine("High Distinction")
Case "A-"
Console.WriteLine("Distinction")
Case "B"
Console.WriteLine("Credit")
Case "C"
Console.WriteLine("Pass")
Case Else
Console.WriteLine("Fail")
End Select
Console.ReadLine()
End Sub
End Module
XII. Results (Output of the Program)
Enter Your grade
A
High Distinction
XIII. Practical Related Questions
1. Write the use of Select Case statement –
- A Select Case statement allows a variable to be tested for equality against a list
of values.
- Each value is called a case, and the variable being switched on is checked for
each select case.
XIV. Exercise
1. Implement the program using Select Case statement to count the number of Vowels
in A to Z alphabets.
Module Module1
Sub Main()
Dim str As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim numberOfVowels As Integer = 0
Practical No 06
Sub Main()
Dim a As Integer =
1 While (a < 10)
Console.WriteLine(
a) a = a + 1
End While
Console.ReadLine()
End Sub
End Module
Output:
1
2
3
4
5
6
7
8
9
For Do loop.
Module Module1
Sub Main()
Dim a As Integer =
1 Do
Console.WriteLine(
a) a = a + 1
Loop While (a<10)
Console.ReadLine()
End Sub
End Module
Output:
1
2
3
4
5
6
7
8
9
Debug.WriteLine("") Debug.WriteLine("")
' Output: 0 1 2 3 4 5 6 7 8 9 10 ' Output: 0 1 2 3 4 5 6 7 8 9 10
Do Loop:
It repeats the enclosed block of statements while a Boolean condition is True or until the
condition becomes True. It could be terminated at any time with the Exit Do statement.
Syntax:
Do
[ statements ]
[ Continue Do ]
[ statements ]
[ Exit Do ]
[ statements ]
Loop { While | Until } condition
XIV. Exercise
1. Write a program using While statement to print the prime numbers between 1 to 100.
Module Module1
Sub Main()
Dim i, j, c As Integer
c=0
i=2
While (i <= 100)
j=2
While (j < i)
If (i Mod j = 0) Then
Exit While
ElseIf (i = j + 1) Then
Console.WriteLine(i)
End If
j=j+1
End While
i=i+1
End While
Console.ReadLine()
End Sub
End Module
OUTPUT:
2. Write a program using While statement to print even-odd numbers between 1 to 50.
Module Module1
Sub Main()
Dim i As Integer = 1
While (i <= 50)
If ((i Mod 2) = 0) Then
Console.WriteLine(i & " Even")
Else
Console.WriteLine(i & " Odd")
End If
i=i+1
End While
Console.ReadLine()
End Sub
End Module
Output:
Practical No 07
Module Module1
Sub Main()
Dim Array() As Integer = {12, 34, 56, 37, 78, 53, _
98, 22, 19, 68}
Dim Key As Integer
Dim IsKeyFoundFlag As Boolean = False
Console.ReadKey()
End Sub
End Module
Sub Main()
For i As Integer = 0 To -10 Step -1
Console.WriteLine(i)
Next
Console.ReadKey()
End Sub
End Module
Output :
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
Sub Main()
Dim i As Single
End Module
XIV. Exercise
1. Write the situation where for each loop statements can be implemented.
Use a For Each...Next loop when you want to repeat a set of statements for each element
of a collection or array.
2. Write program using For Next loop statement tot find the Armstrong
numbers between 1 to 500. (153 is Armstrong Number – 13 + 53 + 33 = 153)
Module Module1
Sub Main()
Dim i As Integer
If (sum = i) Then
Console.WriteLine(i)
End If
Next
Console.ReadLine()
End Sub
End Module
Output:
1
153
370
371
407
Practical No 08
VIII. Resources required (Additional)
If any web resources required.
For i = 1 To num
fact *= i
Next
Factorial = fact
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fact As Long
fact = Factorial(TextBox1.Text)
MessageBox.Show("Factorial of " & TextBox1.Text &
" is " & fact, "Factorial", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End Sub
End Class
XIV. Exercise
1. Write a program to perform the arithmetic operations using controls Label, Button
and TextBox.
2. Write a program to change the background color of the form when user clicks
on different buttons.
Output:
Practical No 09
PictureBox1.Show()
PictureBox2.Hide()
End Sub
End Class
OUTPUT:
XIV. Exercise
1. Write a program to change the ForeColor of the text in Label using
different RadioButtons such Red, Green, Blue.
Practical No 10
XIV. Exercise
Practical No 11
XIV. Exercise
1. Write program using picture box control to load an image at run time.
OUTPUT:
dynamicPanel.Controls.Add(textBox1)
dynamicPanel.Controls.Add(checkBox1)
dynamicPanel.Controls.Add(checkBox2)
Controls.Add(dynamicPanel)
End Sub
End Class
OUTPUT:
Practical No 12
VIII. Resources required (Additional)
If any web resources required.
XIV. Exercise
1. Write a program to display the traffic signal using timer control.
PictureBox1.Visible = True
PictureBox2.Visible = False
PictureBox3.Visible = False
End
If End
Sub
End Sub
End Class
OUTPUT:
Practical No 13 & 14
VIII. Resources required (Additional)
If any web resources required.
Imports System.Text.RegularExpressions
Public Class Form1
End Class
XII. Results (Output of the Program)
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim regex As Regex = New Regex("(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])
\/((19|20)\d\d))$")
Dim b As Boolean = regex.IsMatch(TextBox1.Text.Trim)
If Not b Then
ErrorProvider1.SetError(TextBox1, "Invalid Format")
Else
MsgBox("Valid Format")
ErrorProvider1.SetError(TextBox1, "")
End If
End Sub
End Class
OUTPUT:
XIV. Exercise
1. Write a program using ErrorProvider for username and password authentication.
OUTPUT:
2. Write a program using ErrorProvider for control to validate Mobile Number and
Email ID in GUI appnlication.
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim number As New Regex("\d{10}")
If number.IsMatch(TextBox1.Text) Then
ErrorProvider1.SetError(TextBox1, "")
MsgBox("Valid Phone Number")
Else
ErrorProvider1.SetError(TextBox1, "Invalid Phone Number")
End If
Dim regex As Regex = New Regex("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-
9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
Dim isValid As Boolean = regex.IsMatch(TextBox2.Text.Trim)
If Not isValid Then
ErrorProvider1.SetError(TextBox2, "Invalid E-mail ID")
Else
ErrorProvider1.SetError(TextBox2, "")
MsgBox("Valid E-Mail ID")
End If
End Sub
End Class
GUI Application Development using VB.Net (22034) Page 4
Practical No. 13 & 14: Implement a Windows application to Perform Validation on various control.
OUTPUT:
Practical No 15
Sub Main()
displaymsg()
Dim a, b As Integer
Console.WriteLine("Enter
Value")
a = Console.ReadLine()
b =
Console.ReadLine()
Add(a, b)
Console.ReadLine()
End Sub
Sub displaymsg()
Console.WriteLine("Use of sub-
procedure") End Sub
Sub Add(ByVal a As Integer, ByVal b As Integer)
Console.WriteLine("Addition = " & a + b)
End Sub
End Module
Sub Main()
Dim num As Integer
Console.WriteLine("Enter a
Number") num = Console.ReadLine()
factorial(num)
Console.ReadLine()
End Sub
Sub factorial(ByVal num As
Integer) ' local variable
declaration */ Dim i,
factorial As Integer
factorial = 1
For i = 1 To num
factorial = factorial *
i Next i
Console.WriteLine("Factorial=" & factorial)
Console.ReadLine()
End
Sub End
Module
OUTPUT:
Enter a Number
5
Factorial=120
XIV. Exercise
Sub Main()
Fibonacci(10)
Console.ReadLine()
End Sub
Sub Fibonacci(ByVal n As
Integer) Dim a As Integer =
0
Dim b As Integer =
1 Dim i As
Integer For i = 0
To n - 1
Dim temp As
Integer temp = a
a = b
b = temp + b
Console.WriteLine(
a)
Next
End Sub
End Module
OUTPUT:
1
1
2
3
5
8
13
21
34
55
2. Develop a program to print the reverse of any number using Sub procedure.
Module Module1
Sub Main()
Dim num As Integer
Console.WriteLine("Enter
Number") num =
Console.ReadLine() reverse(num)
Console.ReadLine()
End Sub
Sub reverse(ByVal num As
Practical No 16
XIV. Exercise
1. Write a program to identify maximum number using parameterized function.
(Use two Textbox for input a integer number and display output in Message
Box)
OUTPUT:
Practical No 17
XIV. Exercise
1. Write a program to identify Volume of Box Class, with three data members,
length, breadth and height.
Module Module1
Sub Main()
Dim obj As Box = New Box()
Dim vol As Integer
vol = obj.volume(2, 4, 4)
Console.WriteLine("Length =" &
obj.l) Console.WriteLine("Breadth
=" & obj.b)
Console.WriteLine("Height =" &
obj.h) Console.WriteLine("Volume ="
& vol) Console.ReadLine()
End Sub
Class Box
Public l, b, h As Integer
Function volume(ByVal i As Integer, ByVal j As Integer,
_ByVal k As Integer)
Dim v As
Integer l = i
b = j
h = k
v = l * b *
h Return v
End
Function End
Class
End Module
Output:
2. Implement a program to accept values from Combo Box and Display average of this
in message box using class.
ComboBox2.Items.Add(6)
ComboBox2.Items.Add(1
1)
ComboBox2.Items.Add(1
7)
ComboBox2.Items.Add(2
4)
ComboBox2.Items.Add(3
6)
End Sub
Practical No 18
OUTPUT:
Constructor Executed
This is base class
Module
Module1
Sub
Main()
Dim obj As New syco
obj.show()
End
Sub End
Module
Public Class syco
Public Function show()
Console.WriteLine("This is base
Class")
GUI Application Development using VB.Net (22034) Page 1
Practical No.18: Implement a Program For Class Constructor and Destructor To De-Allocate
Memory.
End Function
Protected Overrides Sub Finalize()
Console.WriteLine("Destructor executing here")
Console.ReadLine()
End
Sub End
Class
OUTPUT:
This is base class
Destructor executing here
XIV. Exercise
1. Implement a program to display any message at run time.(Using Constructor).
Module Module1
Sub Main()
Dim obj As New sample
obj.display()
Console.ReadLine()
End Sub
Class sample
Public Sub New()
Console.WriteLine("This is
Constructor") End Sub
Sub display()
Console.WriteLine("This is
Method") End Sub
End Class
End Module
Output:
OUTPUT:
Area of Circle = 12.56
Practical No 19
Sub Main()
Dim obj As New
details obj.show()
Console.ReadLine()
End
Sub End
Module
Warning2 Property 'FullName' doesn't return a value on all code paths. A null reference
exception could occur at run time when the result is used.
Error 3 Statement cannot appear within a method body. End of method assumed.
XIV. Exercise
1. Implement a program for inheritance where Student is Child Class and faculty is Base
class.(Take Appropriate variable in Base and Child class)
Module Module1
Sub Main()
Dim s As New student
s.branch()
s.year()
Console.ReadLine()
End Sub
Class faculty
Dim b As String =
"Computer" Sub branch()
Console.WriteLine("Branch = " &
b) End Sub
End Class
Class
student
Inherits faculty
Dim y As String = "Second
Year" Sub year()
Console.WriteLine("Year = " &
y) End Sub
End Class
End Module
Output:
Practical No 20 & 21
Sub Main()
Dim res As New addition
Console.WriteLine("Overloaded Values of Class
addition") Console.WriteLine(res.add(10))
Console.WriteLine(res.add(35, 20))
Console.ReadLine()
End Sub
End Module
Output:
Overloaded Values of Class addition
10
55
Overriding:
Module Module1
Sub Main()
Dim obj As New child
Dim result As Integer
result = obj.add(10, 5)
Console.WriteLine("Overloaded Values of Class
addition") Console.WriteLine("Result =" & result)
Console.ReadLine()
End Sub
End Module
Public Class parent
Public Overridable Function add(ByVal i As Integer, ByVal j As Integer)
Return (i + j)
End Function
End Class
Output:
Result of Addition =30
Overloaded Values of Class addition
Result =15
Sub Main()
Dim obj As New
EmpInfo
obj.ShowInfo()
Console.ReadLine()
End Sub
End Module
Public Class
EmpPersonalDetails Dim
name As String
Dim address As String
Public Overridable Function ShowInfo()
Console.WriteLine("Employee Name" & name)
Console.WriteLine("Employee Address" & address)
End
Function End
Class
Public Class EmpInfo
Inherits EmpPersonalDetails
Dim EmpId As Integer
Dim sallary As Integer
Dim JoinDate As Date
Overloads Function ShowInfo()
MyBase.ShowInfo()
Console.WriteLine("Employee ID" &
EmpId)
Console.WriteLine("Employee Sallary" & sallary)
Console.WriteLine("Employee Joining Date" & JoinDate)
End
Function End
Class
OUTPUT:
Employee Name
Employee Address
Employee ID0
Employee Sallary0
Employee Joining Date12:00:00 AM
XIV. Exercise
1. Implement a windows application for show string concatenation using
overload method
End Class
Output:
Practical No 22
Output:
Parent
Parent
Parent
Child
Class Shadow
Shared x As Integer = 1
Shared Sub Main()
Dim x As Integer = 10
Console.WriteLine("main:x" & x)
Console.WriteLine("main shadow x:" &
Shadow.x)
End Sub
End Class
XIV. Exercise
1. Implement the concept of shadowing through inheritance in a console application.
Module Module1
Dim f As New
first
Dim s As New second
Dim t As New third
Sub Main()
f.display()
s.display()
t.display()
Console.ReadLine()
End Sub
End Module
Public Class first
Public Sub display()
Console.WriteLine("This is First Year")
End Sub
End Class
Public Class second
Inherits first
Private Shadows Sub display()
Console.WriteLine("This is Second Year")
End Sub
End Class
Public Class third
Inherits second
Public Shadows Sub display()
Console.WriteLine("This is Third Year")
End Sub
End Class
OUTPUT:
This is First Year
This is First Year
This is Third Year
Practical No 23
End Try
End Sub
End Class
Output:
Module Module1
Sub Main()
Try
Throw New Exception("Mega-error")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Sub
End Module
Output:
Mega-error
Module Module1
Sub Main()
Try
'Try to divide by zero.
Dim value As Integer = 1 / Integer.Parse("0")
'This statement is sadly not reached.
Console.WriteLine("Hi")
Catch ex As Exception
'Display the Message.
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Sub
End Module
Output:
Arithmetic operation resulted in an overflow.
XIV. Exercise
1. Write a program for student registration using exception handling.
OUTPUT:
Practical No 24
Imports System.Data
Imports System.Data.OleDb
End Sub
End Class
Output:
XIV. Exercise
1. Design the windows application that will display the content of a table in MS-
Access database on DataGrid control using data adapter.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
End Sub
End Class
OUTPUT:
Practical No 25 & 26
Imports System.Data
Imports System.Data.OleDb
End Sub
End Class
Output:
2. Write a data adapter syntax using a MS-access code with a student table.
Dim da As OleDbDataAdapter
Da=New OleDbDataAdapter(cmd)
XIV. Exercise
1. Design the windows application in MS-Access which have navigation (Next,
First, Previous, Last).
Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
Dim ds As New DataSet
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Db1DataSet.student' table. You can
move, or remove it, as needed.
Me.StudentTableAdapter.Fill(Me.Db1DataSet.student)
con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\
Users\Suresh\Documents\Visual Studio 2012\Projects\Nav2\db1.mdb")
con.Open()
cmd = New OleDbCommand("Select * From student", con)
da = New OleDbDataAdapter(cmd)
da.Fill(ds, "student")
Me.TextBox1.DataBindings.Add("text", ds, "student.RollNo")
Me.TextBox2.DataBindings.Add("text", ds, "student.Name")
Me.TextBox3.DataBindings.Add("text", ds, "student.Marks")
End Sub
End Sub
2. Develop a windows application that will contain multiple tables in a single dataset.
Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
Dim ds As New DataSet
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
da1.Fill(ds, "student")
da2.Fill(ds, "subjects")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "student"
DataGrid2.DataSource = ds
DataGrid2.DataMember = "subjects"
End Sub
End Class
OUTPUT:
Practical No 27
Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
Dim ds As New DataSet
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Output:
XIV. Exercise
1. Design a simple Windowsform for accepting the details of Employee. Using the
connected architecture of ADO.NET, perform the following operations:
Insert Record
Search Record
Update Record
Delete Record
Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
Dim ds As New DataSet
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Private Sub btnIns_Click(sender As Object, e As EventArgs) Handles btnIns.Click
Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Users\Suresh\Documents\Visual Studio 2012\Projects\Employee\
Employee.mdb")
con.Open()
If txtNo.Text = "" Then
MsgBox("Please Enter Employee Number")
Else
Dim InsertString As String
InsertString = "Insert into Employee(Emp_No, Emp_Name, Address,
Date_of_joining) values('" + txtNo.Text + "','" + txtName.Text + "' ,'" + txtAdd.Text + "','"
+ txtDOJ.Text + "')"
Dim cmd As New OleDbCommand(InsertString, con)
cmd.ExecuteNonQuery()
MsgBox(" Record Successfully Inserted")
txtNo.Clear()
txtName.Clear()
txtAdd.Clear()
txtDOJ.Clear()
con.Close()
End If
End Sub
End Sub
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Employee"
con.Close()
End Sub
OUTPUT:
Imports System.Data.OleDb
Public Class Form1
End Sub
End Class
Output:
XIV. Exercise
1. Design a windows application for student name and college name using a simple
data binding use appropriate database.
Imports System.Data.OleDb
Public Class Form1
OUTPUT:
2. Design a windows application for bank customer record & display it using Complex
data binding use appropriate database.
Imports System.Data.OleDb
Public Class Form1
ComboBox1.DataSource = ds.Tables("Bank_Details")
ComboBox2.DataSource = ds.Tables("Bank_Details")
ComboBox3.DataSource = ds.Tables("Bank_Details")
ComboBox1.DisplayMember = "Acc_No"
ComboBox2.DisplayMember = "Balance"
ComboBox3.DisplayMember = "Branch"
End Sub
End Class
OUTPUT: