Vbnet Programs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Downloaded from: www.sucomputersforum.

com
Visual Basic.Net Practical Solutions
1. Write a program to allow the user to input two integer values and then the
program print the results of adding, subtracting, multiplying, and dividing among
the two values.
Objective of the Program is:
To give a better understanding about arithmetic operators and controls like label and
textbox and button controls

Solution:
In this program we need to add 2 text boxes and 6 labels and 2 buttons
2 text boxes to read 2 values and 2 labels to represent the textboxes and other 4 labels
to display output after calculating Addition, subtraction, multiplication and division of
two numbers entered by user when the respected event is called
Two buttons to represent calculate procedure and exiting procedure of the application
as shown below

Now we need to Change the properties of the controls taken in form window as
following
To change the properties click on the control and go to properties window which
will be displayed in left side of the application window if properties window is
not visible then go to view menu click on other windows option and select
properties window and shortcut is f4

Control Property Value


Button1 text Calculate
Button2 text Exit
Button3 text Clear All(Additionally Added)

Page 1 of 20
Downloaded from: www.sucomputersforum.com

Control Property Value


Label1 text Enter first Number
Label2 text Enter Second Number
Label3 Text Add
Label4 text Subtract
Label5 text Multiply
Label6 text Divide

Note: For all controls change the font property as per self interest
Adjust controls to give a proper designing and set your form to give a pleasant look
if possible set background and foreground color to the form.

Before going to code application save the form and save your project save by selecting save project option from
file menu
Give project name as PROGRAM1
Your project will be saved in
Root directoryusersuser namedocumentsvisual studio 2010 projectsPROGRAM1.

C:\Users\Bee HaPPY\Documents\Visual Studio 2010\Projects\PROGRAM1

Now Code for Button1 i.e Calculate:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Label4.Text = "A+B=" + Str(Int(TextBox1.Text) + Int(TextBox2.Text))
Label5.Text = "A-B =" + Str(Int(TextBox1.Text) - Int(TextBox2.Text))
Label6.Text = "A*B=" + Str(Int(TextBox1.Text) * Int(TextBox2.Text))
Label7.Text = "A/B=" + Str(Int(TextBox1.Text) / Int(TextBox2.Text))
End Sub

Page 2 of 20
Downloaded from: www.sucomputersforum.com
Code for Button2 i.e Exit Button:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
End
End Sub
Code for Button3 i.e Clear All Button:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
Label4.Text = 0
Label5.Text = 0
Label6.Text = 0
Label7.Text = 0
End Sub

To test and run the applications click on Debug menu and select “Start Debugging” option
or you can use shortcut f5
And also you can use standard tool bar start debugging button

Once you closed your project you can open it by selecting open project option from file
menu and select your project from the directory

Thank you for more applications, check our website:


www.sucomputersforum.com

Page 3 of 20
Downloaded from: www.sucomputersforum.com
2. Using windows form application collect the user details like first name, middle name, last name,
address, gender, course name with submit and clear button. Display the user details in another form.
Objective of the program:
This program is to provide an idea how data of one form can be displayed in another form. By seeing
this application we will get an idea that how multiple forms can be used in single project.

Solution:
Add additional form i.e Form2
Add 7 labels 6 Text Boxes and 4 buttons as Shown below

Change the properties as shown below:

Control Property Value


Button1 text Submit
Button2 text Clear All
Button3 text Display
Button4 text Exit
Label1 text Enter first name
Label2 text Enter Middle Name
Label3 Text Enter Last Name
Label4 text Gender
Label5 text Address
Label6 text Course name
Label7 Text User Details

Form2 Controls:
Add 5 labels and one button as shown below

Page 4 of 20
Downloaded from: www.sucomputersforum.com

Note: Change the font property for all the controls (Both form1 & Form2) as your
wish

Declare the variables in general declaration section that is above all events
Dim fn, mn, ln, address, gender, cn As String

Now write the code for Form1 Exit Button:


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
End
End Sub
code for Form1 Clear all Button:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
End Sub

Page 5 of 20
Downloaded from: www.sucomputersforum.com
code for Form1 Submit Button:
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
fn = TextBox1.Text
mn = TextBox2.Text
ln = TextBox3.Text
gender = TextBox4.Text
address = TextBox5.Text
cn = TextBox6.Text
End Sub

write the code for Form1 Display Button:


Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button4.Click
Form2.Label1.Text = "Your Name is" + Me.fn + Me.mn + Me.ln
Form2.Label2.Text = "You are a " + Me.gender
Form2.Label3.Text = "Your Address is" + Me.address
Form2.Label4.Text = "Your Course Name is" + Me.cn
Form2.Show()
End Sub

Now time to write the code for Form2 Exit Button:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
End
End Sub

Once you run this application it takes data of textboxes into variables and displays the given
details in form2.
To test and run the applications click on Debug menu and select “Start Debugging” option
or you can use shortcut f5
And also you can use standard tool bar start debugging button

Once you closed your project you can open it by selecting open project option from file
menu and select your project from the directory

Thank you for more applications, check our website:


www.sucomputersforum.com

Page 6 of 20
Downloaded from: www.sucomputersforum.com

3. Write a program to construct calculator.


Objective of the program: This program is to provide an idea about how a real time
simple calculator works using single display unit where it collects multiple values and
multiple operators.

Solution:
Add 17 buttons and a text box as shown in the picture
set the properties to display it as simple calculator
Note: Change the Text Align property of text box to right

Code for it is:

Public Class Calculator


Dim fn As Decimal
Dim sn As Decimal
Dim operations As Integer
Dim op_sel As Boolean = False

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button1.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "1"
Else
TextBox1.Text = "1"
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button2.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "2"
Else
TextBox1.Text = "2"
End If
Page 7 of 20
Downloaded from: www.sucomputersforum.com
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button3.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "3"
Else
TextBox1.Text = "3"
End If

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button4.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "4"
Else
TextBox1.Text = "4"
End If

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button5.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "5"
Else
TextBox1.Text = "5"
End If

End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button6.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "6"
Else
TextBox1.Text = "6"
End If

End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button7.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "7"
Else
TextBox1.Text = "7"
End If

End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button8.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "8"
Else
TextBox1.Text = "8"
End If

End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button9.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "9"
Else
TextBox1.Text = "9"
End If Page 8 of 20
Downloaded from: www.sucomputersforum.com

End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button10.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "0"
End If

End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button11.Click
If Not (TextBox1.Text.Contains(".")) Then
TextBox1.Text += "."
End If
End Sub

Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button17.Click
TextBox1.Text = "0"

End Sub

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button12.Click
fn = TextBox1.Text
TextBox1.Text = "0"
op_sel = True
operations = 1

End Sub

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button13.Click
fn = TextBox1.Text
TextBox1.Text = "0"
op_sel = True
operations = 2
End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button14.Click
fn = TextBox1.Text
TextBox1.Text = "0"
op_sel = True
operations = 3
End Sub

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button15.Click
fn = TextBox1.Text
TextBox1.Text = "0"
op_sel = True
operations = 4
End Sub

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button16.Click
If op_sel = True Then
sn = TextBox1.Text
If operations = 1 Then
TextBox1.Text = fn + sn
ElseIf operations = 2 Then
TextBox1.Text = fn - sn
ElseIf operations = 3 Then
TextBox1.Text = fn * sn
Else
If sn = 0 Then Page 9 of 20
Downloaded from: www.sucomputersforum.com
TextBox1.Text = "Error..."
Else
TextBox1.Text = fn / sn
End If
End If
op_sel = False
fn = 0
sn = 0
End If
End Sub

End Class

There is one more way of writing as to create a control array kind of code. Where multiple
buttons handles similar code
In this we need to write a common code for all the 16 buttons except “=” button like
1,2,3,4,5,6,7,8,9,0,.,+,-,/,* ,AC, etc.,
As shown below.
Public class form1
Private k,v,op as string
Private x,y as double

Private sub button1_click((ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button1.click, Button2.click, Button3.click, Button4.click, Button5.click, Button6.click,
Button7.click, Button8.click, Button9.click, Button10.click, Button11.click, Button12.click,
Button13.click, Button14.click, Button15.click, Button16.click,

K=sender.text
If k=”AC” then
X=0
Y=0
K= “”
V= “”
Op= “”
End If
If k=”+” or k=”-“ or k=”*” or k=”/” then
If op=”+” then textbox1.text=x+y
If op=”-” then textbox1.text=x-y
If op=”*” then textbox1.text=x*y
If op=”/” then textbox1.text=x/y
Op=k
X=val(Textbox1.text)
V=””
Else
V=v+k
Textbox1.text=val(v)
Y=val(textbox1.text)
Endif
Endsub Page 10 of 20
Downloaded from: www.sucomputersforum.com
End class

To test and run the applications click on Debug menu and select “Start Debugging” option
or you can use shortcut f5
And also you can use standard tool bar start debugging button

Output:

Once you closed your project you can open it by selecting open project option from file
menu and select your project from the directory

Thank you for more applications, check our website:


www.sucomputersforum.com

6. Write a program to simulate Traffic Signals.


Objective of the Program is:
To give an understanding about timer controls

Solution:
In this program we need to add a timer control from components category of toolbox to
count the time in seconds by setting interval property to 1000, and 3 ovalshapes with
same radius from visual basic powerpacks category to represent three signals viz, red,
yellow, green and a text box to display the the countdown timer for the application as
shown below

Page 11 of 20
Downloaded from: www.sucomputersforum.com

Now change the shapes positions and resizing to required output of the program to look
like a traffic signal as shown below.

Now change the properties of ovalshapes and text box control

Control Property Value


Ovalshape1 Fillcolor Red
Ovalshape2 Fillcolor Yellow
Oval shape3 Fillcolor Green
Timer1 interval 1000
Textbox1 Enabled false
Textbox1 Font Cambria 13 Bold
Textbox1 textalign Center

Page 12 of 20
Downloaded from: www.sucomputersforum.com
Code:
Public Class Form1
Dim c As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Timer1.Start()
c = 18
TextBox1.Text = c
Timer1.Interval = 1000
OvalShape1.BackColor = Color.Red
OvalShape2.BackColor = Color.Yellow
OvalShape3.BackColor = Color.Green
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
OvalShape2.FillStyle = PowerPacks.FillStyle.Transparent
OvalShape3.FillStyle = PowerPacks.FillStyle.Transparent
End Sub
Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
TextBox1.Text = c
If OvalShape1.FillStyle = PowerPacks.FillStyle.Solid And c = 0 Then
OvalShape1.FillStyle = PowerPacks.FillStyle.Transparent
OvalShape2.FillStyle = PowerPacks.FillStyle.Solid
c = 6
End If
If OvalShape2.FillStyle = PowerPacks.FillStyle.Solid And c = 0 Then
OvalShape2.FillStyle = PowerPacks.FillStyle.Transparent
OvalShape3.FillStyle = PowerPacks.FillStyle.Solid
c = 12
End If
If OvalShape3.FillStyle = PowerPacks.FillStyle.Solid And c = 0 Then
OvalShape3.FillStyle = PowerPacks.FillStyle.Transparent
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
c = 18
End If
c = c - 1
End Sub
End Class

To test and run the applications click on Debug menu and select “Start Debugging” option
or you can use shortcut f5
And also you can use standard tool bar start debugging button

Output:

Page 13 of 20
Downloaded from: www.sucomputersforum.com

Once you closed your project you can open it by selecting open project option from file
menu and select your project from the directory

Thank you for more applications, check our website:


www.sucomputersforum.com

7. Write a program to sort an integer array of 10 elements in ascending.


Console Application program:
Module Module1
Sub Main()
'Dim k, a, nele As Integer
Dim arr() = {12, 23, 1, 21, 12, 12, 32, 45, 3, 5}
bubbleSort(arr, arr.Length)
For a = 0 To arr.Length - 1
Console.Write("{0,4}", arr(a))
Next
Console.ReadLine()
End Sub
Sub bubbleSort(ByVal d() As Integer, ByVal n As Integer)
Dim i, j As Integer
For i = 0 To n Step 1
For j = n - 1 To i + 1 Step -1
If (d(j) < d(j - 1)) Then
Dim temp As Integer = d(j)
d(j) = d(j - 1)
d(j - 1) = temp
End If
Next
Next
End Sub
End Module
Output:
1 3 5 12 12 12 21 23 32 45

Page 14 of 20
Downloaded from: www.sucomputersforum.com

8. Write a program that determines a student’s grade.

Objective of the program:


This program aims at providing an idea behind valuating Grade of a student by accepting
student’s marks of the subjects.

Solution:
This program needs 18 labels and 6 text boxes, and a button
Among which 6 text boxes are used to enter the marks
6 labels are used to represent subject names
6 labels are used to grades of individual subjects
1 is used to display title
3 are used to display headings
1 to display result label
1 is to display result
1 button for calculation procedure

Now change the properties as shown below

Control Property Value


Label14 Text Student Grade Calculator
Label15 Text Subject
Label16 text Grade
Label1 Text Telugu
Label2 Text Hindi
Label3 Text English
Label4 Text Maths
Label5 Text Science
Label6 Text Social
Label13 Text Your GPA is:
Button1 text Calculate GPA

Arrange them as shown below

Page 15 of 20
Downloaded from: www.sucomputersforum.com

Code:

Public Class Form1


Dim tel, eng, hin, mat, sci, soc, a, b, c, d, f, g As Integer
Dim gpa As Double

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button1.Click
tel = CInt(TextBox1.Text)
hin = CInt(TextBox2.Text)
eng = CInt(TextBox3.Text)
mat = CInt(TextBox4.Text)
sci = CInt(TextBox5.Text)
soc = CInt(TextBox6.Text)

If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or


TextBox5.Text = "" Or TextBox6.Text = "" Then
MsgBox("GPA cannot be calculated due to one or more subject marks is Not entered")

Else

If tel > 90 And tel <= 100 Then


Label7.Text = "A1"
a = 10
ElseIf tel > 80 And tel <= 90 Then
Label7.Text = "A2"
a=9
ElseIf tel > 70 And tel <= 80 Then
Label7.Text = "B1"
a=8
ElseIf tel > 60 And tel <= 70 Then
Label7.Text = "B2"
a=7
ElseIf tel > 50 And tel <= 60 Then
Label7.Text = "C1"
a=6
Page 16 of 20
Downloaded from: www.sucomputersforum.com
ElseIf tel > 40 And tel <= 50 Then
Label7.Text = "C2"
a=5
ElseIf tel > 35 And tel <= 40 Then
Label7.Text = "D"
a=4
ElseIf tel >= 0 And tel <= 35 Then
Label7.Text = "E"
a=0
ElseIf tel < 0 Or tel > 100 Then
Label7.Text = "Err...."
a=0
End If

If eng > 90 And eng <= 100 Then


Label9.Text = "A1"
c = 10
ElseIf eng > 80 And eng <= 90 Then
Label9.Text = "A2"
c=9
ElseIf eng > 70 And eng <= 80 Then
Label9.Text = "B1"
c=8
ElseIf eng > 60 And eng <= 70 Then
Label9.Text = "B2"
c=7
ElseIf eng > 50 And eng <= 60 Then
Label9.Text = "C1"
c=6
ElseIf eng > 40 And eng <= 50 Then
Label9.Text = "C2"
c=5
ElseIf eng > 35 And eng <= 40 Then
Label9.Text = "D"
c=4
ElseIf eng >= 0 And eng <= 35 Then
Label9.Text = "E"
c=0
ElseIf eng < 0 Or eng > 100 Then
Label9.Text = "Err...."
c=0
End If

If mat > 90 And mat <= 100 Then


Label10.Text = "A1"
d = 10
ElseIf mat > 80 And mat <= 90 Then
Label10.Text = "A2"
d=9
ElseIf mat > 70 And mat <= 80 Then
Label10.Text = "B1"
d=8
Page 17 of 20
Downloaded from: www.sucomputersforum.com
ElseIf mat > 60 And mat <= 70 Then
Label10.Text = "B2"
d=7
ElseIf mat > 50 And mat <= 60 Then
Label10.Text = "C1"
d=6
ElseIf mat > 40 And mat <= 50 Then
Label10.Text = "C2"
d=5
ElseIf mat > 35 And mat <= 40 Then
Label10.Text = "D"
d=4
ElseIf mat >= 0 And mat <= 35 Then
Label10.Text = "E"
d=0
ElseIf mat < 0 Or mat > 100 Then
Label10.Text = "Err...."
d=0
End If

If sci > 90 And sci <= 100 Then


Label11.Text = "A1"
f = 10
ElseIf sci > 80 And sci <= 90 Then
Label11.Text = "A2"
f=9
ElseIf sci > 70 And sci <= 80 Then
Label11.Text = "B1"
f=8
ElseIf sci > 60 And sci <= 70 Then
Label11.Text = "B2"
f=7
ElseIf sci > 50 And sci <= 60 Then
Label11.Text = "C1"
f=6
ElseIf sci > 40 And sci <= 50 Then
Label11.Text = "C2"
f=5
ElseIf sci > 35 And sci <= 40 Then
Label11.Text = "D"
f=4
ElseIf sci >= 0 And sci <= 35 Then
Label11.Text = "E"
f=0
ElseIf sci < 0 Or sci > 100 Then
Label11.Text = "Err...."
f=0
End If

If soc > 90 And soc <= 100 Then


Label12.Text = "A1"
g = 10
ElseIf soc > 80 And soc <= 90 Then
Label12.Text = "A2"
Page 18 of 20
Downloaded from: www.sucomputersforum.com
g=9
ElseIf soc > 70 And soc <= 80 Then
Label12.Text = "B1"
g=8
ElseIf soc > 60 And soc <= 70 Then
Label12.Text = "B2"
g=7
ElseIf soc > 50 And soc <= 60 Then
Label12.Text = "C1"
g=6
ElseIf soc > 40 And soc <= 50 Then
Label12.Text = "C2"
g=5
ElseIf soc > 35 And soc <= 40 Then
Label12.Text = "D"
g=4
ElseIf soc >= 0 And soc <= 35 Then
Label12.Text = "E"
g=0
ElseIf soc < 0 Or soc > 100 Then
Label12.Text = "Err...."
g=0
End If

If hin > 89 And hin <= 100 Then


Label8.Text = "A1"
b = 10
ElseIf hin > 78 And hin <= 89 Then
Label8.Text = "A2"
b=9
ElseIf hin > 67 And hin <= 78 Then
Label8.Text = "B1"
b=8
ElseIf hin > 56 And hin <= 67 Then
Label8.Text = "B2"
b=7
ElseIf hin > 45 And hin <= 56 Then
Label8.Text = "C1"
b=6
ElseIf hin > 35 And hin <= 45 Then
Label8.Text = "C2"
b=5
ElseIf hin > 20 And hin <= 35 Then
Label8.Text = "D"
b=4
ElseIf hin >= 0 And hin <= 20 Then
Label8.Text = "E"
b=0
ElseIf hin < 0 Or hin > 100 Then
Label8.Text = "Err...."
b=0
End If

Page 19 of 20
Downloaded from: www.sucomputersforum.com
If Label7.Text = "Err...." Or Label8.Text = "Err...." Or Label9.Text = "Err...." Or Label10.Text =
"Err...." Or Label11.Text = "Err...." Or Label12.Text = "Err...." Then
Label6.Text = "Fail"
ElseIf Label7.Text = "E" Or Label8.Text = "E" Or Label9.Text = "E" Or Label10.Text = "E" Or
Label11.Text = "E" Or Label12.Text = "E" Then
Label6.Text = "Fail"
Else

gpa = (a + b + c + d + f + g) / 6
Label6.Text = gpa
End If
End If
End Sub

End Class

Page 20 of 20

You might also like