0% found this document useful (0 votes)
18 views36 pages

VB.net

The document outlines various programming tasks in Visual Basic, including creating GUIs for basic arithmetic operations, checking for vowels, voting eligibility, and generating Fibonacci series. It also demonstrates the use of options like Strict, Compare, and Explicit, along with matrix operations and dynamic arrays. Each task includes code snippets and descriptions of functionality, showcasing practical applications of VB.NET programming concepts.

Uploaded by

Gaurav singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views36 pages

VB.net

The document outlines various programming tasks in Visual Basic, including creating GUIs for basic arithmetic operations, checking for vowels, voting eligibility, and generating Fibonacci series. It also demonstrates the use of options like Strict, Compare, and Explicit, along with matrix operations and dynamic arrays. Each task includes code snippets and descriptions of functionality, showcasing practical applications of VB.NET programming concepts.

Uploaded by

Gaurav singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

SWAMI RAMA HIMALAYAN

UNIVERSITY HILL CAMPUS


TOLI DUDARKHAL

PROGRAMM 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 times 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
6. 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
8.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
10.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
12. 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
16. 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
5.Demomstrate the use of Option with strict ,compare & explicit.

I. Option Strict.

Option Strict On ' Enforces strict type checking

Module Program
Sub Main()
' This will give a compile-time error because Option Strict is on
' Dim x As Integer = "123" ' Implicit conversion from String to Integer

' Correct way


Dim y As Integer = Convert.ToInt32("123") ' Explicit conversion
Console.WriteLine(y) ' Output: 123
End Sub
End Module

II. Option compare.

Option Compare Binary ' String comparison is case-sensitive

Module Program
Sub Main()
Dim str1 As String = "apple"
Dim str2 As String = "Apple"

' Since Option Compare Binary is on, this comparison is case-sensitive


If str1 = str2 Then
Console.WriteLine("Strings are equal")
Else
Console.WriteLine("Strings are different") ' This will be printed
End If
End Sub
End Module

III. Option explicit.

Option Explicit On ' Forces explicit declaration of variables

Module Program
Sub Main()
' This will give a compile-time error because the variable is not declared
' x = 10

' Correct way


Dim x As Integer = 10
Console.WriteLine(x) ' Output: 10
End Sub
End Module
7. Design and code for addition, subtraction and multiplication of two matrices of 2*2.
Public Module Program

Public Sub Main(args() As string)

Dim ar As Integer(,)=New Integer(2,2){{2,4},{5,8}}

Dim br As Integer(,)=New Integer(2,2){{1,2},{3,5}}

Dim sum As Integer(,)=New Integer(2,2){}

Dim subtract As Integer(,)=New Integer(2,2){}

Dim i,j As Integer

for i=0 to 1

for j=0 to 1

sum(i,j)=br(i,j)+ar(i,j)

subtract(I,j)=ar(I,j)-br(i,j)

Next j

Next i

console.WriteLine("the sum of two array is")

for i=0 to 1

for j=0 to 1

console.WriteLine(sum(i,j))

Next j

Next i

console.WriteLine("the subtraction of two array is")

for i=0 to 1

for j=0 to 1

console.WriteLine(subtract(i,j))

Next j

Next i

console.ReadLine()

End Sub

End Module
Output:
the sum of two array is
3

13

the subtraction of two array is


1

3
9.Design and code to check whether the entered no is Armstrong and palindrome or
not.
Module Program

' Function to check if a number is Armstrong

Function IsArmstrong(ByVal num As Integer) As Boolean

Dim temp As Integer = num

Dim sum As Integer = 0

Dim numberOfDigits As Integer = num.ToString().Length

While temp > 0

Dim digit As Integer = temp Mod 10

sum += Math.Pow(digit, numberOfDigits)

temp \= 10

End While

Return sum = num

End Function

' Function to check if a number is a palindrome

Function IsPalindrome(ByVal num As Integer) As Boolean

Dim original As Integer = num

Dim reverse As Integer = 0

While num > 0

Dim digit As Integer = num Mod 10

reverse = reverse * 10 + digit

num \= 10

End While

Return original = reverse

End Function

' Main program


Sub Main()

Console.Write("Enter a number: ")

Dim input As Integer = Convert.ToInt32(Console.ReadLine())

' Check if the number is Armstrong and Palindrome

Dim isArmstrong As Boolean = IsArmstrong(input)

Dim isPalindrome As Boolean = IsPalindrome(input)

' Output the results

If isArmstrong Then

Console.WriteLine($"{input} is an Armstrong number.")

Else

Console.WriteLine($"{input} is not an Armstrong number.")

End If

If isPalindrome Then

Console.WriteLine($"{input} is a Palindrome number.")

Else

Console.WriteLine($"{input} is not a Palindrome number.")

End If

' Wait for the user to press a key before closing

Console.ReadKey()

End Sub

End Module
Output:

153 is an Armstrong number.

153 is a Palindrome number.


11. Design and code an application for dynamic array that accepts a array for the user
and add it to a list box. Preserve the values by selectiong check box.

Public Class Form1

' Declare a list to store values

Dim preservedValues As New List(Of String)

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

Dim userInput As String = txtInput.Text

' Split the input into an array by commas

Dim values As String() = userInput.Split(","c)

' Add values to ListBox and preserve them in the list

For Each value As String In values

ListBox1.Items.Add(value.Trim())

preservedValues.Add(value.Trim()) ' Add to preserved list

Next

' Clear the input box

txtInput.Clear()

End Sub

' Button click to remove selected items from ListBox

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click

' Remove selected item from ListBox and preserved list

If ListBox1.SelectedItem IsNot Nothing Then

Dim selectedItem As String = ListBox1.SelectedItem.ToString()

ListBox1.Items.Remove(selectedItem)

preservedValues.Remove(selectedItem)

End If

End Sub
' Checkbox changed event to preserve ListBox contents

Private Sub chkPreserve_CheckedChanged(sender As Object, e As EventArgs) Handles


chkPreserve.CheckedChanged

If chkPreserve.Checked Then

' Add all preserved values to ListBox

ListBox1.Items.Clear()

For Each value In preservedValues

ListBox1.Items.Add(value)

Next

Else

' Optionally clear the ListBox if unchecked

ListBox1.Items.Clear()

End If

End Sub

' Optional: Saving data to a file when closing the application (to preserve across sessions)

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles


MyBase.FormClosing

If chkPreserve.Checked Then

' Save preserved values to a file when closing (if needed)

System.IO.File.WriteAllLines("preservedData.txt", preservedValues)

End If

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

If System.IO.File.Exists("preservedData.txt") Then

preservedValues = System.IO.File.ReadAllLines("preservedData.txt").ToList()

For Each value As String In preservedValues

ListBox1.Items.Add(value)

Next

End If

End Sub

End Class

Output:
13. Design and code for insertion sorting by using a user define sub-routine.

Imports system
Module Module1

Sub Main()
' Example array to be sorted
Dim numbers As Integer() = {29, 10, 14, 37, 13}

' Call the InsertSort subroutine to sort the array


Console.WriteLine("Original Array:")
PrintArray(numbers)

' Sorting the array using Insertion Sort


InsertionSort(numbers)

' Print the sorted array


Console.WriteLine("Sorted Array:")
PrintArray(numbers)

Console.ReadLine() ' To pause and see the output


End Sub

' Subroutine to perform Insertion Sort


Sub InsertionSort(ByRef arr As Integer())
Dim n As Integer = arr.Length
Dim i As Integer, key As Integer, j As Integer

' Loop through the array starting from the second element
For i = 1 To n - 1
key = arr(i)
j=i-1

' Move elements of arr(0..i-1), that are greater than key, to one position ahead
While j >= 0 AndAlso arr(j) > key
arr(j + 1) = arr(j)
j -= 1
End While

' Place the key at the correct position


arr(j + 1) = key
Next
End Sub

' Subroutine to print the array


Sub PrintArray(ByVal arr As Integer())
For Each num As Integer In arr
Console.Write(num & " ")
Next
Console.WriteLine()
End Sub
End Module
Output:
Original Array:
29 10 14 37 13

Sorted Array:
10 13 14 29 37
14.Design and code for Selection sorting by using a user-define sub-routine.

Imports System

Module Program
Sub Main()
' Initialize an array with unsorted values
Dim numbers As Integer() = {64, 25, 12, 22, 11}

' Display the original array


Console.WriteLine("Original Array:")
DisplayArray(numbers)

' Call the SelectionSort subroutine to sort the array


SelectionSort(numbers)

' Display the sorted array


Console.WriteLine("Sorted Array:")
DisplayArray(numbers)

' Wait for user input before closing


Console.ReadLine()
End Sub

' Subroutine to implement the Selection Sort algorithm

Sub SelectionSort(ByRef arr As Integer())


Dim n As Integer = arr.Length

' Traverse through all array elements


For i As Integer = 0 To n - 2
' Find the minimum element in the unsorted part of the array
Dim minIndex As Integer = i

For j As Integer = i + 1 To n - 1
If arr(j) < arr(minIndex) Then
minIndex = j
End If
Next

If minIndex <> i Then


Dim temp As Integer = arr(i)
arr(i) = arr(minIndex)
arr(minIndex) = temp
End If
Next
End Sub

' Subroutine to display the array


Sub DisplayArray(ByVal arr As Integer())
For Each num As Integer In arr
Console.Write(num & " ")
Next
Console.WriteLine()
End Sub
End Module

Output:
15.Design and code for Bubble sorting by using a user-define sub-routine.

Imports System

Module Program

Sub Main()

' Initialize an array with unsorted values

Dim numbers As Integer() = {64, 34, 25, 12, 22, 11}

' Display the original array

Console.WriteLine("Original Array:")

DisplayArray(numbers)

' Call the BubbleSort subroutine to sort the array

BubbleSort(numbers)

' Display the sorted array

Console.WriteLine("Sorted Array:")

DisplayArray(numbers)

' Wait for user input before closing

Console.ReadLine()

End Sub

' Subroutine to implement the Bubble Sort algorithm

Sub BubbleSort(ByRef arr As Integer())

Dim n As Integer = arr.Length

Dim swapped As Boolean

' Traverse through all array elements

For i As Integer = 0 To n - 2

swapped = False

' Last i elements are already sorted, so no need to check them

For j As Integer = 0 To n - i - 2
' Compare adjacent elements

If arr(j) > arr(j + 1) Then

' Swap if the element found is greater than the next element

Dim temp As Integer = arr(j)

arr(j) = arr(j + 1)

arr(j + 1) = temp

swapped = True

End If

Next

' If no two elements were swapped in the inner loop, then the array is sorted

If Not swapped Then

Exit For

End If

Next

End Sub

' Subroutine to display the array

Sub DisplayArray(ByVal arr As Integer())

For Each num As Integer In arr

Console.Write(num & " ")

Next

Console.WriteLine()

End Sub

End Module
Output:

You might also like