0% found this document useful (0 votes)
39 views7 pages

Visual Basic .NET Code Examples

The document contains multiple Visual Basic .NET code snippets demonstrating various functionalities such as basic arithmetic operations, quadratic equation solving, palindrome checking, and sorting names. Each code segment includes event handlers for buttons that perform specific tasks when clicked. The examples illustrate user input handling, conditional statements, and list management within a graphical user interface.

Uploaded by

chidziwosteven
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)
39 views7 pages

Visual Basic .NET Code Examples

The document contains multiple Visual Basic .NET code snippets demonstrating various functionalities such as basic arithmetic operations, quadratic equation solving, palindrome checking, and sorting names. Each code segment includes event handlers for buttons that perform specific tasks when clicked. The examples illustrate user input handling, conditional statements, and list management within a graphical user interface.

Uploaded by

chidziwosteven
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

VISUAL BASIC.

NET CODES
PublicClassForm1

PrivateSub Button1_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
Dim num1, num2, sum AsInteger
num1 = InputBox("Enter first number")
num2 = InputBox("Entr second number")
sum = num1 + num2
[Link] = sum
EndSub

PrivateSub Button2_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
[Link] = Val([Link]) +
Val([Link])
EndSub

PrivateSub Button3_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
Dim num1, num2, total AsInteger
num1 = [Link]
num2 = [Link]
total = Val(num1) + Val(num2)
[Link] = total

EndSub
EndClass

________________________________________________________________
PublicClassForm1

PrivateSub Button1_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
Dim num1 AsInteger, num2 AsInteger, num3 AsInteger
num1 = [Link]
num2 = [Link]
num3 = num1 + num2
[Link] = num3
[Link] = num3

EndSub
______________________________________________________________
______

PrivateSub Form1_Load(ByVal sender As [Link], ByVal e


As [Link]) [Link]
[Link] = [Link]
EndSub
PrivateSub Button2_Click(ByVal sender As [Link], ByVal
e As [Link]) Handles [Link]
Dim x AsInteger, p AsInteger
For x = 0 To 100
p = x Mod 2
If p = 1 Then
[Link](x)
Else

EndIf
Next
EndSub
EndClass
______________________________________________________________
______

ModuleModule1

Sub Main()
Dim num1, num2, total As Integer
[Link]("Enter first number")
num1 = [Link]()
[Link]("Enter second number")
num2 = [Link]()
total = num1 + num2
[Link]("Total is " & total)
[Link]()
EndSub

EndModule

______________________________________________________________
______

PublicClassForm1
PrivateSub Button1_Click(ByVal sender As [Link], ByVal
e As [Link]) Handles [Link]
[Link]()
Dim a, b, c AsInteger, x, y AsDouble
a = InputBox("Enter the value for variable a")
b = InputBox("Enter the value for variable b")
c = InputBox("Enter the value for variable c")
If (b ^ 2 - 4 * a * c) > 0 Then
x = (-b + [Link](b ^ 2 - 4 * a * c)) / 2 * a
y = (-b - [Link](b ^ 2 - 4 * a * c)) / 2 * a
[Link]("The first root for the
equation is "& y)
[Link]("The second root for the
equation is "& x)

ElseIf (b ^ 2 - 4 * a * c) = 0 Then
x = (-b) / 2 * a
[Link]("The only root for the equation
is "& x)
Else
MsgBox("The has no real roots")
EndIf

EndSub
EndClass
________________________________________________________________
PublicClassForm1

PrivateSub Button1_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
Dim names AsString
Dim count AsInteger
Dim names1 AsString
names1 = ""
names = InputBox("Enter a string")
For count = Len(names) To 1 Step -1
names1 = names1 + Mid(names, count, 1)
Next count
If names = names1 Then
[Link]("The word "& names &" is a
palindrome")
MsgBox("The word "& (names) &" is a palindrome")
Else
[Link]("The word "& names &" is not a
palindrome")
MsgBox("The word "& (names) &" is not a
palindrome")
EndIf
EndSub
______________________________________________________________
________________________________
PrivateSub Button2_Click(ByVal sender As [Link], ByVal
e As [Link]) Handles [Link]
Dim x, y AsInteger
For x = 620 To 500 Step -1
y = x Mod 2
If y = 1 Then
[Link](x)
Else
[Link](x)
EndIf
Next
EndSub
EndClass

________________________________________________________________
ModuleModule1
Function Add(ByVal a, ByVal b) AsInteger
Add = a + b
EndFunction
Sub Main()
Dim fn, sn, answer AsInteger
[Link]("Enter first number")
fn = [Link]()
[Link]("Enter first number")
sn = [Link]()
answer = Add(fn, sn)
[Link]("The sum is "& answer)
[Link]()
EndSub

EndModule
________________________________________________________________
PublicClassForm1

PrivateSub Button1_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
Dim x, mark AsInteger
For x = 1 To 2
mark = InputBox("Enter a mark")
If mark < 0 Or mark > 100 Then
MsgBox("Enter a mark from 0 to 100 only")
Else
SelectCase mark
CaseIs>= 70 ', Is <= 100
'MsgBox("Grade A")
[Link]("The grade of the
mark "& mark &" is A")
Case 65 To 69
'MsgBox("Grade B")
[Link]("The grade of the
mark "& mark &" is B")
Case 55 To 64
'MsgBox("Grade C")
[Link]("The grade of the
mark "& mark &" is C")
Case 50 To 54
'MsgBox("Grade D")
[Link]("The grade of the
mark "& mark &" is D")
Case 40 To 49
'MsgBox("Grade E")
[Link]("The grade of the
mark "& mark &" is E")
Case 35 To 39
'MsgBox("Grade O")
[Link]("The grade of the
mark "& mark &" is O")
Case Else
'MsgBox("Grade F")
[Link]("The grade of the
mark "& mark &" is F")
EndSelect
EndIf

Next

If mark < 0 Or mark > 100 Then x = -1


EndSub
______________________________________________________________
________________________
PrivateSub Button2_Click(ByVal sender As [Link], ByVal
e As [Link]) Handles [Link]
Dim x, mark AsInteger
x = 0
DoWhile x < 2
mark = InputBox("Enter a mark")
If mark > 0 And mark <= 100 Then
SelectCase mark
CaseIs>= 70 ', Is <= 100
'MsgBox("Grade A")
[Link]("The grade of the
mark "& mark &" is A")
Case 65 To 69
'MsgBox("Grade B")
[Link]("The grade of the
mark "& mark &" is B")
Case 55 To 64
'MsgBox("Grade C")
[Link]("The grade of the
mark "& mark &" is C")
Case 50 To 54
'MsgBox("Grade D")
[Link]("The grade of the
mark "& mark &" is D")
Case 40 To 49
'MsgBox("Grade E")
[Link]("The grade of the
mark "& mark &" is E")
Case 35 To 39
'MsgBox("Grade O")
[Link]("The grade of the
mark "& mark &" is O")
Case Else
'MsgBox("Grade F")
[Link]("The grade of the
mark "& mark &" is F")
EndSelect

EndIf
x += 1
Loop
EndSub
__
______________________________________________________________
________________________
PrivateSub Button3_Click(ByVal sender As [Link], ByVal
e As [Link]) Handles [Link]
Dim x, mark AsInteger
x = 0
Do
mark = InputBox("Enter a mark")
If mark > 0 And mark <= 100 Then
SelectCase mark
CaseIs>= 70 ', Is <= 100
'MsgBox("Grade A")
[Link]("The grade of the
mark "& mark &" is A")
Case 65 To 69
'MsgBox("Grade B")
[Link]("The grade of the
mark "& mark &" is B")
Case 55 To 64
'MsgBox("Grade C")
[Link]("The grade of the
mark "& mark &" is C")
Case 50 To 54
'MsgBox("Grade D")
[Link]("The grade of the
mark "& mark &" is D")
Case 40 To 49
'MsgBox("Grade E")
[Link]("The grade of the
mark "& mark &" is E")
Case 35 To 39
'MsgBox("Grade O")
[Link]("The grade of the
mark "& mark &" is O")
Case Else
'MsgBox("Grade F")
[Link]("The grade of the
mark "& mark &" is F")
EndSelect
EndIf
x += 1
LoopUntil x = 2
EndSub
______________________________________________________________
________________________
PrivateSub Button4_Click(ByVal sender As [Link], ByVal
e As [Link]) Handles [Link]
Dim names(4) AsString
Dim x, y AsInteger, temp AsString
x = 0
DoWhile x < 5
names(x) = InputBox("Enter a name")
[Link](names(x))
x += 1
Loop
For x = 0 To 4
For y = 0 To 3
If names(y) > names(y + 1) Then
temp = names(y)
names(y) = names(y + 1)
names(y + 1) = temp
EndIf
Next
Next
x = 0
[Link]()
[Link]()
Do
[Link](names(x))
[Link](names(x))

x = x + 1
LoopUntil x = 5
EndSub

______________________________________________________________
________________________
PrivateSub Button5_Click(ByVal sender As [Link], ByVal
e As [Link]) Handles [Link]
Dim names(4) AsString
Dim x AsInteger
x = 0
[Link]()
DoWhile x < 5
names(x) = InputBox("Enter a name")
[Link] = True
x += 1
Loop
EndSub
EndClass

Common questions

Powered by AI

The grading system is implemented by collecting numerical marks from the user within a loop and evaluating them with Select Case statements. Each mark falls into predefined ranges that correspond to specific grades; for instance, marks at 70 or above receive Grade A, while marks from 65 to 69 receive Grade B, and so on with distinct grade assignments for each range . The code checks for valid input (marks between 0 and 100) and uses the ListBox to display the resulting grade alongside the input mark . This selective branching allows efficient categorization and outputting of grades.

The Visual Basic .NET code ensures user input validation through conditions checking input range. For the grading system, it checks whether the entered mark is within 0 to 100. If an invalid input is detected (less than 0 or greater than 100), the program displays a message urging the user to "Enter a mark from 0 to 100 only" . Additionally, attempts to input integers are wrapped with text field input and conversion functions (e.g., Val function), ensuring data integrity and correctness before calculations are conducted, thereby supporting reliable and intuitive user experience . This highlights a proactive approach in basic error handling and user guidance.

The code contains a loop that iterates over a specified range (from 0 to 100), using a modulus operation to check each number's parity. The modulus operation `x Mod 2` determines if `x` is odd (result is 1). When a number is odd, it is added to a ListBox control . A separate implementation decrements from 620 to 500, similarly adding odd numbers to one ListBox and even numbers to another based on the parity check . This logic encapsulates efficient control and display of selected elements based on arithmetic computations in a defined range.

The code prompts the user to input a string and utilizes a loop to reverse it. By iterating from the last character to the first, it constructs a reversed version of the original string using the `Mid` function within a loop that appends each character in reverse order . Post iteration, the code compares the original string with the reversed version. If they are identical, the input is identified as a palindrome, prompting the display of a message and list entry verifying this . If they differ, a corresponding non-palindrome message and list entry is generated. This process demonstrates string traversal and comparison techniques.

The code dynamically changes the interface aesthetics by setting the form's background color upon loading. The `Form1_Load` event explicitly sets the `BackColor` property to `Color.Aquamarine` when the form initializes, thereby altering the visual appearance to enhance user experience . This example demonstrates a straightforward yet effective method to customize graphical user interface components, providing an element of visual differentiation that can enhance user's interaction with an application.

The Visual Basic .NET code employs conditional statements to solve a quadratic equation using inputs for variables a, b, and c. It calculates the discriminant (`b^2 - 4ac`) to determine the nature of the roots. If the discriminant is positive, it calculates two distinct real roots using the quadratic formula and displays them in a ListBox . If the discriminant is zero, it computes and displays the sole real root. When the discriminant is negative, indicating no real roots, a message box informs the user that there are no real roots. This implementation demonstrates proper use of conditionals to evaluate mathematical expressions and control program flow.

The Visual Basic .NET code uses multiple methods to handle integer calculations. In one example, it employs the InputBox function to obtain two integers from the user, adds these integers, and displays the result using a Label control by modifying its Text property . Another method leverages two TextBox controls to receive numeral input, convert them into integers using the Val function, and display the sum also in a TextBox . Additionally, another subroutine reads numbers from TextBox controls, calculates the sum, and then updates another TextBox with the result . These methods collectively demonstrate user interaction for input and output in a GUI environment.

The algorithm implemented in the Visual Basic .NET code demonstrates a bubble sort technique for sorting a list of names. It starts by collecting five names via InputBox and storing them in an array . The code then uses a nested loop structure where the outer loop runs for each element and the inner loop compares adjacent elements, swapping them if they are out of order. This ensures that the largest unsorted element is moved towards the end of the array over successive iterations . Following sorting, the names are displayed in a ListBox, which highlights the transformation from a simple list to a sorted one. This algorithm effectively illustrates the concept of bubble sort within a graphical application setup.

You might also like