SWAMI RAMA HIMALAYAN
UNIVERSITY HILL CAMPUS
TOLI DUDARKHAL
PROGRAMME NAME:-BCA
COURSE NAME: VB.NET
COURSE CODE: BCP 302
SUBMITTED TO: SUBMITTED BY:
ASHISH BAUTHIYAL GAURAV
ASSISTANT PROFESSOR REG NO.:PG22111301005
1. Create GUI from Show below and Type Code for Each Button.
Public Class Form1
Dim a, b, c As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = TextBox1.Text
b = TextBox2.Text
c=a+b
TextBox3.Text = c
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
a = TextBox1.Text
b = TextBox2.Text
c=a-b
TextBox3.Text = c
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
a = TextBox1.Text
b = TextBox2.Text
c=a*b
TextBox3.Text = c
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
a = TextBox1.Text
b = TextBox2.Text
c=a/b
TextBox3.Text = c
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
End Sub
End Class
2. Write a program in Visual basic to find whether a given character is vowel or
not .Display the result in a message box.
Imports System.Net.Configuration
Public Class Form1
Dim ch As Char
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ch = TextBox1.Text
Select Case ch
Case "A", "E", "i", "O", "U"
MessageBox.Show(ch + "is vowel in Upper case")
Case "a", "e", "i", "o", "u"
MessageBox.Show(ch + " is vowel lower case")
Case Else
MessageBox.Show(ch + " is not vowel")
End Select
End Sub
End Class
3. Create GUI From Show Below and check the eligibility for Voting.
Public Class Form1
Dim age As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
age = Val(t1.Text)
If age >= 18 Then
MessageBox.Show("You are eligible to vote")
Else
MessageBox.Show("You are not eligible to vote")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
t1.Text = ""
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Close()
End Sub
End Class
4. Show that how many time a page has been clicked ?
Imports System.Net.Security
Public Class Form1
Private Sub btnCount_Click(sender As Object, e As EventArgs) Handles btnCount.Click
Static clickcount As Integer = 0
clickcount += 1
If clickcount = " 50" Then
clickcount = 1
End If
Label1.Text = clickcount.ToString("00")
End Sub
End Class
5. Create GUI Form Show Below.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Val(TextBox1.Text) > Val(TextBox2.Text) Then
TextBox3.Text = "First number is Biggest"
Else
TextBox3.Text = " Second number is Biggest"
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles
TextBox1.TextChanged
End Sub
End Class
6. Design and code to print a Table of a given no in textbox.
Public Class Form1
Dim mu As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
While (mu <= 10)
ListBox1.Items.Add(Val(TextBox1.Text) & "*" & mu & "=" & mu * Val(TextBox1.Text))
mu += 1 'mu= mu+1'
ListBox1.Refresh()
End While
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
ListBox1.Refresh()
TextBox1.Text = ""
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Me.Close()
End Sub
End Class
7. Design and code a Calculator by using Control Array.
Public Class Form1
Dim value1, value2 As Integer
Dim sign As String
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = TextBox1.Text & 2
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text & 1
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = TextBox1.Text & 3
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
TextBox1.Text = TextBox1.Text & 4
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
TextBox1.Text = TextBox1.Text & 5
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
TextBox1.Text = TextBox1.Text & 6
End Sub
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
TextBox1.Text = TextBox1.Text & 7
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
TextBox1.Text = TextBox1.Text & 8
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
TextBox1.Text = TextBox1.Text & 9
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
value1 = TextBox1.Text
TextBox1.Clear()
TextBox1.Focus()
sign = "+"
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
value1 = TextBox1.Text
TextBox1.Clear()
TextBox1.Focus()
sign = "-"
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
value1 = TextBox1.Text
TextBox1.Clear()
TextBox1.Focus()
sign = "*"
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
value1 = TextBox1.Text
TextBox1.Clear()
TextBox1.Focus()
sign = "/"
End Sub
Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
TextBox1.Text = TextBox1.Text & 0
End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
value2 = TextBox1.Text
If sign = "+" Then
TextBox1.Text = value1 + value2
ElseIf sign = "-" Then
TextBox1.Text = value1 - value2
ElseIf sign = "*" Then
TextBox1.Text = value1 * value2
Else
TextBox1.Text = value1 / value2
End If
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
TextBox1.Clear()
TextBox1.Focus()
End Sub
End Class
8. Design and code to generate fibonacii series on a label upto entered limit.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim A, B, C, N As Integer
N = Val(TextBox1.Text)
A=0
B=1
C=A
TextBox2.Text = A & " " & B & " "
For I = 2 To N - 1
C=A+B
TextBox2.Text = TextBox2.Text & C & " "
A=B
B=C
Next I
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
End
End Sub
End Class
9. Design and code to return the factorial of a given no by using user define
function.
Imports System.Drawing.Imaging
Public Class Form1
Function fact(a As Integer)
Dim f As Integer
f=1
For i = 1 To a
f=f*i
Next
fact = f
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim n As Integer, r As Integer
n = TextBox1.Text
r = fact(n)
TextBox2.Text = r
End Sub
End Class
10. Design and coding for selection sorting by using a user define sub routine in
vb.net.
Module Module1
Sub Main()
Dim numbers As Integer() = {5, 2, 9, 1, 5,
6} Console.WriteLine("Original Array:")
PrintArray(numbers)
InsertionSort(numbers)
Console.WriteLine("Sorted Array:")
PrintArray(numbers)
Console.ReadLine() ' To keep the console window open
End Sub
Sub InsertionSort(ByRef arr As Integer())
Dim n As Integer = arr.Length
Dim i, j, key As Integer
For i = 1 To n - 1
key = arr(i)
j=i-1
While j >= 0 AndAlso arr(j) > key
arr(j + 1) = arr(j)
j -= 1
End While
arr(j + 1) =
key
Next
End Sub
Sub PrintArray(arr As Integer())
For Each num As Integer In arr
Console.Write(num & " ")
Next
Console.WriteLine()
End Sub
End Module
Module SelectionSortDemo
Sub Main()
Dim arr() As Integer = {64, 25, 12, 22, 11}
Console.WriteLine("Original array:")
PrintArray(arr)
SelectionSort(arr)
Console.WriteLine("Sorted array:")
PrintArray(arr)
Console.ReadLine()
End Sub
Sub SelectionSort(ByRef array() As Integer)
Dim n As Integer = array.Length
For i As Integer = 0 To n - 2
Dim minIndex As Integer = i
For j As Integer = i + 1 To n - 1
If array(j) < array(minIndex) Then
minIndex = j
End If
Next
If minIndex <> i Then
Dim temp As Integer = array(i)
array(i) = array(minIndex)
array(minIndex) = temp
End If
Next
End Sub
Sub PrintArray(ByVal array() As Integer)
For Each item As Integer In array
Console.Write(item & " ")
Next
Console.WriteLine()
End Sub
End Module
11. Design and coding for insertion sorting by using a user define sub routine in
vb.net.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim inputArray As String() = TextBox1.Text.Split(New String() {Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries)
' Convert the string array to integer array
Dim intArray As Integer() = Array.ConvertAll(inputArray, AddressOf Integer.Parse)
' Call the InsertionSort subroutine
InsertionSort(intArray)
' Display the sorted result in TextBox2
TextBox2.Text = String.Join(Environment.NewLine, intArray)
End Sub
' Subroutine for insertion sort
Private Sub InsertionSort(ByRef arr As Integer())
Dim n As Integer = arr.Length
For i As Integer = 1 To n - 1
Dim key As Integer = arr(i)
Dim j As Integer = i - 1
While j >= 0 AndAlso arr(j) > key
arr(j + 1) = arr(j)
j -= 1
End While
arr(j + 1) =
key
Next
End Sub
End Class
12. Design and coding for bubble sorting by using a user define sub routine in
vb.net.
Module BubbleSortExample
Sub Main()
Dim numbers() As Integer = {64, 34, 25, 12, 22, 11, 90}
Console.WriteLine("Original Array:")
DisplayArray(numbers)
BubbleSort(numbers)
Console.WriteLine("Sorted Array:")
DisplayArray(numbers)
' Wait for user input before closing
Console.WriteLine("Press any key to exit...")
Console.ReadKey()
End Sub
Sub BubbleSort(ByRef arr() As Integer)
Dim n As Integer = arr.Length
Dim temp As Integer
For i As Integer = 0 To n - 1
For j As Integer = 0 To n - 2 - i
If arr(j) > arr(j + 1) Then
next
temp = arr(j)
arr(j) = arr(j + 1)
arr(j + 1) = temp
End If
Next
Next
End Sub
Sub DisplayArray(arr() As Integer)
For Each num As Integer In arr
Console.Write(num & " ")
Next
Console.WriteLine()
End Sub
End Module
13. Demonstrate the use of option with strict , compare and explicit in vb.net.
Module Program
Sub Main()
' Example of Option Explicit
Dim number As Integer = 10 ' Explicitly declaring the variable
Dim text As String = "Hello"
' This will cause a compile-time error if Option Explicit is On:
' message = "This will cause an error!" ' Uncommenting this will cause an error
Console.WriteLine(CompareStrings(text, "hello")) ' Returns True due to Option Compare
Text
Console.WriteLine(CompareNumbers(number, 10)) ' Returns True
' Example of Option Strict (commented out to avoid runtime errors)
' Dim value As Integer = "123" ' This will cause a compile-time error with Option Strict On
Console.ReadLine()
End Sub
Function CompareStrings(ByVal str1 As String, ByVal str2 As String) As Boolean
' Strings will be compared in a case-insensitive manner
Return str1.Equals(str2)
End Function
Function CompareNumbers(ByVal num1 As Integer, ByVal num2 As Integer) As
Boolean ' Direct comparison, safe with Option Strict On
Return num1 = num2
End Function
End Module
14. Designing and code for addition, subtraction and multiplication of two
matrices of 2*2 in vb.net
Module MatrixOperations
Sub Main()
' Declare two 2x2 matrices
Dim matrixA(1, 1) As Integer
Dim matrixB(1, 1) As Integer
' Input values for the first matrix
Console.WriteLine("Enter values for the first 2x2 matrix (A):")
For i As Integer = 0 To 1
For j As Integer = 0 To 1
Console.Write($"Enter value for A({i},{j}): ")
matrixA(i, j) = Convert.ToInt32(Console.ReadLine())
Next
Next
' Input values for the second matrix
Console.WriteLine(vbCrLf & "Enter values for the second 2x2 matrix (B):")
For i As Integer = 0 To 1
For j As Integer = 0 To 1
Console.Write($"Enter value for B({i},{j}): ")
matrixB(i, j) = Convert.ToInt32(Console.ReadLine())
Next
Next
' Display results of operations
Console.WriteLine(vbCrLf & "Matrix A:")
DisplayMatrix(matrixA)
Console.WriteLine(vbCrLf & "Matrix B:")
DisplayMatrix(matrixB)
Console.WriteLine(vbCrLf & "Addition (A + B):")
Dim sumResult(1, 1) As Integer = AddMatrices(matrixA, matrixB)
DisplayMatrix(sumResult)
Console.WriteLine(vbCrLf & "Subtraction (A - B):")
Dim diffResult(1, 1) As Integer = SubtractMatrices(matrixA, matrixB)
DisplayMatrix(diffResult)
Console.WriteLine(vbCrLf & "Multiplication (A * B):")
Dim multResult(1, 1) As Integer = MultiplyMatrices(matrixA, matrixB)
DisplayMatrix(multResult)
' Wait for user input before closing
Console.WriteLine(vbCrLf & "Press any key to exit...")
Console.ReadKey()
End Sub
Sub DisplayMatrix(matrix(,) As Integer)
For i As Integer = 0 To 1
For j As Integer = 0 To 1
Console.Write(matrix(i, j) & vbTab)
Next
Console.WriteLine()
Next
End Sub
Function AddMatrices(A(,) As Integer, B(,) As Integer) As Integer(,)
Dim result(1, 1) As Integer
For i As Integer = 0 To 1
For j As Integer = 0 To 1
result(i, j) = A(i, j) + B(i, j)
Next
Next
Return result
End Function
Function SubtractMatrices(A(,) As Integer, B(,) As Integer) As Integer(,)
Dim result(1, 1) As Integer
For i As Integer = 0 To 1
For j As Integer = 0 To 1
result(i, j) = A(i, j) - B(i, j)
Next
Next
Return result
End Function
Function MultiplyMatrices(A(,) As Integer, B(,) As Integer) As Integer(,)
Dim result(1, 1) As Integer
'Matrix multiplication logic for 2x2 matrices
result(0, 0) = A(0, 0) * B(0, 0) + A(0, 1) * B(1, 0)
result(0, 1) = A(0, 0) * B(0, 1) + A(0, 1) * B(1, 1)
result(1, 0) = A(1, 0) * B(0, 0) + A(1, 1) * B(1, 0)
result(1, 1) = A(1, 0) * B(0, 1) + A(1, 1) * B(1, 1)
Return result
End Function
End Module
15. Design and code to check wheather the entered no. is armstrong and
palindrome or not in vb.net.
Public Class Form1
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
Dim input As String = txtNumber.Text
Dim number As Integer
' Try to convert input to an integer
If Integer.TryParse(input, number) Then
Dim isArmstrong As Boolean = CheckArmstrong(number)
Dim isPalindrome As Boolean = CheckPalindrome(number)
' Display results
Dim result As String = $"Is Armstrong: {isArmstrong}" & Environment.NewLine &
$"Is Palindrome: {isPalindrome}"
MessageBox.Show(result)
Else
MessageBox.Show("Please enter a valid integer.")
End If
End Sub
Private Function CheckArmstrong(num As Integer) As Boolean
Dim sum As Integer = 0
Dim digits As Integer = num.ToString().Length
Dim temp As Integer = num
While temp > 0
Dim digit As Integer = temp Mod 10
sum += Math.Pow(digit, digits)
temp \= 10
End While
Return sum = num
End Function
Private Function CheckPalindrome(num As Integer) As Boolean
Dim original As String = num.ToString()
Dim reversed As String = StrReverse(original)
Return original = reversed
End Function
End Class
16. Design and code an application for dynamic array that accept array for the
user and add it to a list box. preserve the values by selecting checkbox in vb.net
Public Class Form1
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim input As String = txtInput.Text.Trim()
If Not String.IsNullOrEmpty(input) Then
' Split the input string into an array (by comma for example)
Dim values As String() = input.Split(","c)
' Add values to the ListBox
For Each value As String In values
Dim trimmedValue As String = value.Trim()
If Not lstItems.Items.Contains(trimmedValue) AndAlso Not
String.IsNullOrEmpty(trimmedValue) Then
lstItems.Items.Add(trimmedValue)
End If
Next
' Clear the TextBox after adding
txtInput.Clear()
End If
End Sub
Private Sub lstItems_SelectedIndexChanged(sender As Object, e As EventArgs) Handles
lstItems.SelectedIndexChanged
' Clear the checked list box
clbPreservedValues.Items.Clear()
' Add the selected items to the checked list box
For Each item As Object In lstItems.SelectedItems
clbPreservedValues.Items.Add(item, True) ' Check by default
Next
End Sub
End Class