1Visual Basic Notes
Some Important Project In Visual Basic
- Make a project to print simple text on the form?
Create a form and place object on the form.
1.FormCaption “Display”
2.Command ButtonNamecmd1
Caption“Print”
Attach the following code with cmd1 button.
Private Sub cmd1_Click()
Print "This is Test Of Print statement”
End Sub
- Make a project to Get Two Number Through two Text Boxes and then add them and then display the result in third text box.
Create a form and place object on the form.
1.FormCaption “Additions”
2.Command ButtonNamecmd1
Caption“Add”
3.Text BoxNametxt1
4.Text BoxNametxt2
5.Text BoxNametxt3
Define following variable and attach the following code with cmd1.
Dim n1 As Integer
Dim n2 As Integer
Dim sum As Integer
Private Sub cmd1_Click ( )
n = txt1.Text
n2 = txt2.Text
sum = n1 + n2
txt3.Text = sum
End Sub
- Make a project to Get a Number Through Text Box and then calculate the Factorial of the number and then display the result in 2nd text box.
Create a form and place object on the form.
1.FormCaption “Factorial”
2.Command ButtonNamecmd1
Caption“Calculate”
3.Text BoxNametxt1
4.Text BoxNameTxt2
Declare the variable and add the
Following code with cmd1.
Dim Fcat As Integer
Private Sub cmd1_Click()
Dim i As Integer
Dim n As Integer
fact = 1
n = txt1.Text
For i = n To 1 Step -1
fact = fact * i
Next i
txt2.Text = fact
End Sub
- Make a project to Get a name, Total marks, Marks Obtained through input box and then calculate the %age and grade. After this display the name, total marks, %age and grade.
Create a form and place object on the form.
Form1Caption“Result”
Label1Caption“name”
Label1Caption“Total Marks”
Label1Caption“Marks Obtained”
Label1Caption“%age”
Label1Caption“Grade”
Command ButtonCaption“Result”
Add the following code with command button1.
Dim n As String
Dim tm As Integer
Dim mo As Integer
Dim p As Integer
Dim g As String
Private Sub Command1_Click()
n = InputBox("enter the name", "For Result card")
tm = InputBox("enter the total marks", "For Result card")
mo = InputBox("enter the Marks Obtained", "For Result card")
p = (mo * 100) / tm
Select Case p
Case Is > 80
g = "a"
Case Is > 70
g = "b"
Case Is > 60
g = "c"
Case Is > 50
g = "d"
Case Is > 33
g = "e"
Case Else
g = "Fail"
End Select
Text1.Text = n
Text2.Text = tm
Text3.Text = mo
Text4.Text = p
Text5.Text = g
End Sub
- Enter the 3 digit Number and then calculate the sum for example 123 sum is 6.
Create a form and place object on the form.
1.FormCaption “Sum”
2.Command ButtonNamecmd1
Caption“Sum”
3.Text BoxNametxt1
4.Text BoxNameTxt2
5.LabelName lbl1
Caption“Enter number Hare”
6.LabelNamelbl2
Caption“Sum is”
Add the following cod with cmd1.
Dim n As Integer
Dim r As Integer
Dim sum As Integer
Private Sub Command1_Click()
sum = 0
n = Text1.Text
While (n > 0)
r = n Mod 10
sum = sum + r
n = n / 10
Wend
Text2.Text = sum
End Sub
- Make a project in whih we move the circle left, right, top and down?
Create a form and place object on the form.
Form1Caption“Movement”
Command ButtonCaption“Left”
Command ButtonCaption“up”
Command ButtonCaption“down”
Command ButtonCaption“Right”
Shapenameshpcircle
Shapecircle
Add the following code with command1, Command 2, Command3,and Command4 respectively.
Const Distance = 500
Private Sub Command1_Click()
shpcircle.Move shpcircle.Left - Distance
End Sub
Private Sub Command2_Click()
shpcircle.Move shpcircle.Left, shpcircle.Top - Distance
End Sub
Private Sub Command3_Click()
shpcircle.Move shpcircle.Left, shpcircle.Top + Distance
End Sub
Private Sub Command4_Click()
shpcircle.Move shpcircle.Left + Distance
End Sub
- Make a project in which get the two number with help of two text box and then select the choice addition, subtraction, multiplication and division with help of option button. After this result is displayed in 3rd text box.
Create a form and place object on the form.
Form1Caption“Movement”
Option button1Caption“addition”
Option button2Caption“subtraction”
Option button3Caption“multiplication”
Option button4Caption“Division”
Text box1caption““
Text box2caption““
Text box3caption““
Label1caption“Enter first number“
Label2caption“Enter f2nd number“
Label3caption“result“
Add the following code with option1, option2, option3 and option4.
Dim n1 As Integer
Dim n2 As Integer
Dim r As Integer
Private Sub Option1_Click()
n1 = Val(Text1.Text)
n2 = Val(Text2.Text)
r = n1 + n2
Label3.Caption = "result of " & Option1.Caption & " is"
Text3.Text = r
End Sub
Private Sub Option2_Click()
n1 = Val(Text1.Text)
n2 = Val(Text2.Text)
r = n1 - n2
Label3.Caption = "result of " & Option2.Caption & " is"
Text3.Text = r
End Sub
Private Sub Option3_Click()
n1 = Val(Text1.Text)
n2 = Val(Text2.Text)
r = n1 * n2
Label3.Caption = "result of " & Option3.Caption & " is"
Text3.Text = r
End Sub
Private Sub Option4_Click()
n1 = Val(Text1.Text)
n2 = Val(Text2.Text)
r = n1 / n2
Label3.Caption = "result of " & Option4.Caption & " is"
Text3.Text = r
End Sub
- Make a project in which we enter the number through the input box and the sort in acceding order. After this unsorted and sorted list displayed in label box?
Create a form and place object on the form.
Form1Caption“Movement”
Command ButtonCaption“Get number”
Command ButtonCaption“sorted”
Label1Caption“”Unsorted list”
Label2Caption“sorted list”
Add the following code with command button1.
Dim num(1 To 5) As Integer
Dim i As Integer
Dim j As Integer
Dim temp As Integer
Dim record As String
Dim sort As String
Private Sub Command1_Click()
' Hare is input
For i = 1 To 5
num(i) = InputBox("enter the number", n)
record = record & Chr(13) & num(i)
Next i
Label2.Caption = record
End Sub
Private Sub Command2_Click()
' hare sorting is start
For i = 1 To 4
For j = i + 1 To 5
If (num(i) > num(j)) Then
temp = num(i)
num(i) = num(j)
num(j) = temp
End If
Next j
Next i
' hare is print the sorted list
For i = 1 To 5
sort = sort & Chr(13) & num(i)
Next i
Label4.Caption = sort
End Sub
9.Calculate the Factorial of given number. Using Do While loop.?
Create following object and set properties.
Form Option Project of Factorial
Command ButtonCaptionFactorial
Text box Text Factorial
LabelName Lbl1
CaptionFactorial
Add following code with cmd1 .
private sub cmd1_click( )
Dim f As long , n As long
n= input box (‘Enter a number ” ,Fact )
F=1
Do while (n >1)
F = f *m
N=n-1
Loop
Text1 .text =f
End sub
10.Develop a project to print the first ten natural number. Usin Do Until Loop.?
Create a form containing following object:-
FormcaptionNumbers
Command Button namecmd1
CaptionDisplay
Attach the following code with cmd1
Private sub cmd1_click( )
Dim n as integer
N=1
Do until n >10
Print n
N=n+1
Loop
End sub
11.Develop a project that calculate and display the sum of first ten- even number. Using While Loop.
1.Create a form and palce following object
FormcaptionSum of first 10 even number
Command button captionDisplay
Namecmd1
Text Boxcaption““
Label Boxname lbl1
Caption Hare is sum of even number
2.Attach the following code with cmd1 button
Private sub cmd1_click( )
Dim r as integer
Dim n as integer
Dim s as integer
S=0
N=1
Do
R=n mod 2
If r=0 then
S= s + n
End if
N=n+1
Loop while n<=10
Text1.text=s
End sub
12.Write a project for calculate and display sum of first ten odd member. Using Do Loop Until.?
1.Create a form and place following object:
Formcaption Sum of odd number
Labelcaption Hare is sum of Fist 10 odd number
Text boxcaption““
Command ButtonCaptionDisplay
Namecmd1
2.Add the following code with cmd1
Private sub cmd1_click( )
Dim r as integer
Dim n as integer
Dim s as integer
S=0
N=1
Do
R=n mod 2
If r > 0 then
S= s + n
End if
N=n+1
Loop Until < 10
Text1.text=s
End sub
13.1.Get the value by text box. Find the factorial and display the Input below in the label1?
2.Get the value by input box and find the factorial and display the result by out put message box. ?
1.Create a form and place following object:
FormCaptionFactorial program
Command buttonNamecmd1
CaptionCalculate
Text boxcaption“Hare enter the number“
LabelNameLbl1
Caption“Result is=“
LabelNameLbl2
Caption““
2.Attach the code with cmd1 button.
Private subcmd1_click( )
Dim n As Integer
Dim fact As long
Dim I As long
N=text1.text
Fact=1
For I= 1 to n
Fact=fact*I
Next I
Lbl2.caption=fact
End Sub
2-Part
1.Create a form and place following object:
FormCaptionFactorial program
Command buttonNamecmd1
CaptionCalculate
2.Attach the code with cmd1 button.
Private subcmd1_click( )
Dim n As Integer
Dim fact As long
Dim I As long
N=inputbox(“enter the number “ ,”Factorial”)
Fact=1
For I= 1 to n
Fact=fact*I
Next I
Lbl2.caption=fact
End
- Get the number through the input box and calculate the factorial using function?
Create a form and place object on the form.
1.FormCaption “Factorial”
2.Command ButtonNamecmd1
Caption“Get”
3.Command ButtonNamecmd2
Caption“Calculate”
4.label1Caption“The factorial is”
5Label2caption““
Add the following code with cmd1 and cmd2.
Dim n As Integer
Dim f As Integer
Private Sub Command1_Click()
n = InputBox("Enter the number", "Input")
End Sub
Private Sub Command2_Click()
f = fact(n)
Label2.Caption = f
End Sub
Public Function fact(x As Integer) As Integer
Dim i As Integer
Dim temp As Integer
temp = 1
For i = 1 To x
temp = temp * i
Next i
fact = temp
End Function
- Get the number through the input box and calculate power using sub procedure?
Create a form and place object on the form.
1.FormCaption “Factorial”
2.Command ButtonNamecmd1
Caption“Get”
3.Command ButtonNamecmd2
Caption“Calculate”
4.label1Caption“power of number is”
5Label2caption““
add the following code with cmd1 and cmd2.
Dim n As Integer
Dim p As Integer
Private Sub Command1_Click()
n = InputBox("Enter the number", "Input")
p = InputBox("enter the power", "Input")
End Sub
Private Sub Command2_Click()
Call power(n, p)
End Sub
Public Sub power(x As Integer, y As Integer)
Dim r As Integer
Dim i As Integer
r = 1
For i = 1 To y
r = r * x
Next i
Label2.Caption = r
End Sub
- Write the project get a number and display the table of this number. Using Do While Wend loop.?
Create the form and set the properties.
Form1Caption “Table “
Command ButtonCaption“Print Table”
Write the following code with form load event.
Dim n as integer
Private Sub Form_Load( )
N=input box(“enter the number”,”input”)
End sub
Write the following code with command1.
Private sub command1_click( )
Dim t as integer
Dim x as integer
T=1
While t <= 10
Print N , “x” ,T , “=” , N * T
T=T+1
Wend
End sub
Note:-
Do While Wend Work always on True.
Do While Loop Work Always on True.
Do-Until loop Work Always on False.
Do—Loop while Work Always On True.
Do-Loop Until Work Always on False.
Compiled by http: