Professional Documents
Culture Documents
Vbnet Programs
Vbnet Programs
Vbnet Programs
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
Page 1 of 20
Downloaded from: www.sucomputersforum.com
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 directoryusersuser namedocumentsvisual studio 2010 projectsPROGRAM1.
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
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
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
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
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
Page 6 of 20
Downloaded from: www.sucomputersforum.com
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
End Sub
End Sub
End Sub
End Sub
End Sub
End Sub
End Sub
End Sub
End Sub
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
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
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.
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
Page 14 of 20
Downloaded from: www.sucomputersforum.com
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
Page 15 of 20
Downloaded from: www.sucomputersforum.com
Code:
Else
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