0% found this document useful (0 votes)
28 views40 pages

Visual Basic Programming Assignments Guide

The document contains 14 programming assignments related to creating VB forms and writing code to perform various tasks. Assignment 1 involves creating a form to add two numbers. Assignment 2 creates a login form. Later assignments involve calculating areas and interests, working with lists and images, and writing functions. Procedures include properties for controls, coding for events like button clicks, and using concepts like loops, variables, and conditional statements.

Uploaded by

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

Visual Basic Programming Assignments Guide

The document contains 14 programming assignments related to creating VB forms and writing code to perform various tasks. Assignment 1 involves creating a form to add two numbers. Assignment 2 creates a login form. Later assignments involve calculating areas and interests, working with lists and images, and writing functions. Procedures include properties for controls, coding for events like button clicks, and using concepts like loops, variables, and conditional statements.

Uploaded by

ranaindia2011
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Assignment 1 :Write a Program to add two numbers by using two text box, three labels control and

one command button.

Proprties :Control

Properties

Textbox 1

Seeting

name
Caption
name
Caption
name
Caption
name

Textbox 2
Label1
Command button

txtfirst
-------txtsecond
-------lblresult
--------cmdsum

caption

&sum

Coding:Private Sub cmdsum_Click()


[Link] = Val([Link]) + Val([Link]) End sub

Assignment 2 :Designe a Login form

Proprties :-

Control

Properties

Seeting

Textbox1

name
Caption
name
Caption
name
Caption

txtusername
--------txtpassword
--------cmdlogin
Login

Textbox2
Command Button

Option Explicit
Dim strMyusername As String, strMyPassword As String
Private Sub cmdLogin_Click()
'Variable declaration
Dim strUserName As String, strPassword As String
Dim intResponse As Integer
Static intAttempt As Integer
'Check if user attempted 3 times then exit the application
If intAttempt = 2 Then
MsgBox "You have made 3 wrong attempt" _
2

& "so now application will have to close immediately"


End
End If
'Fetch the username and password in from txtUserName and txtPassword
respectively
strUserName = [Link]
strPassword = [Link]
'check wheather given username and password is match or not
If strUserName = strMyusername And strPassword = strMyPassword Then
MsgBox "Welcome to your account", , "Welcome"
End
Else
intAttempt = intAttempt + 1
intResponse = MsgBox("Invalid username or password" & vbCrLf _
& "Would you like to retry ?", vbRetryCancel, "Login Error")
If intResponse = vbRetry Then
[Link]
Else
End
End If
End If
End Sub
Private Sub Form_Load()
'Set the username and password
strMyusername = "Brajmohan"
strMyPassword = "Gamer"
End Sub
Private Sub txtPassword_GotFocus()
[Link] = 0
[Link] = Len([Link])
End Sub
Private Sub txtUserName_GotFocus()
[Link] = 0
[Link] = Len([Link])
End Sub

Assignment 4 :Write a program to calculate areaf of circle , the value is input by using inputbox.

Proprties :-

Control

Properties

Command Button

name
Caption
name
Caption
name
Caption

Command Button
Label

Seeting
command1
Area
command2
&Input Value
lblresult
----------

Coding :Dim a As Double


Private Sub Command1_Click()
Dim b As Double
b = 3.14 * a * a
[Link] = b
End Sub

Private Sub Command2_Click()


a = InputBox("Enter The Radious Of the circle", "RADIOUS ")
End Sub

Assignment 5 :Write a program to print the even number between 0-25.

Proprties :-

Control

Properties

Seeting

Command Button

name

cmdeven

Coding :Private Sub cmdeven_Click()


Dim a As Integer, i As Integer
For i = 1 To 25 Step 1
If i Mod 2 = 0 Then
Print i
End If
Next
End Sub

Assignment 6:Input any number and print the table of that number.

Proprties :-

Control

Properties

Seeting

Label

name
Caption
name
Caption
name
Text
name
name
Caption

label1
&Enter The Number
label2
&Table
txtnumber
---------lsttable
cmdtable
&table

Label
Text Box
List Box
Command Button

Coding :Private Sub cmdtable_Click()


Dim a As Integer, i As Integer, c As Integer
a = Val([Link])
For i = 1 To 10 Step 1
c=a*i
[Link] c
Next
End Sub

Assignment 7 :Input Day if week in number and show through message box its corresponding day of
week in words.

Proprties :-

Control

Properties

Seeting

Label

name
Caption
name
Text
name
Caption

label1
No Of Week
txtnumber
---------cmdday
&Corresponding Day

Text Box
Command Button

Coding :Private Sub cmdday_Click()


Dim a As Integer
a = Val([Link])
If a = 1 Then
MsgBox " This is sunday", vbOKOnly, "Day"
ElseIf a = 2 Then
MsgBox " This is Monday", vbOKOnly, "Day"
ElseIf a = 3 Then
MsgBox " This is Tuesday", vbOKOnly, "Day"
ElseIf a = 4 Then
MsgBox " This is Wednesday", vbOKOnly, "Day"
ElseIf a = 5 Then
MsgBox " This is Thrusday", vbOKOnly, "Day"
ElseIf a = 6 Then
MsgBox " This is friday", vbOKOnly, "Day"
ElseIf a = 7 Then
MsgBox " This is saturday", vbOKOnly, "Day"
Else
MsgBox "This is Invalid Number", vbOKOnly, "Day"
End If
End Sub

10

Assignment 8:Design a list box and add the items entered in the text box.

Proprties :-

Control

Properties

Seeting

Label

name
Caption
name
Text
name
Caption
name

label1
&Item
txtnumber
------cmdadd
&ADD
lstlist

Text Box
Command Button
List box

Coding :Private Sub cmdadd_Click()


Dim a
a = [Link]
[Link] a
End Sub

11

Assignment 9 :Design a two list box and to copy all the selected items from a multi select list box to
another list box.

Proprties :-

Control

Properties

Seeting

Label

List Box

name
Caption
name
Caption
name
Caption
name
Caption
name
Caption
name

label1
add Item
cmdadd1
&ADD
cmdadd2
&ADD
cmdcopy
&copy to Left
command1
c&opy to Right
list1

List Box

name

list2

Command Button
Command Button
Command Button
Command Button

12

Coding :Private Sub cmdadd1_Click()


[Link] [Link]
End Sub
Private Sub cmdadd2_Click()
[Link] [Link]
End Sub
Private Sub cmdcopy_Click()
Dim i As Integer
For i = [Link] - 1 To 0 Step -1
If [Link](i) Then
[Link] ([Link](i))
End If
Next
End Sub
Private Sub Command1_Click()
Dim i As Integer
For i = [Link] - 1 To 0 Step -1
If [Link](i) Then
[Link] ([Link](i))
End If
Next
End Sub

13

Assignment 10:Input a string INDIA and show in this Way.


I
IN
IND
INDI
INDIA
INDI
IND
IN
I

Proprties :-

Control

Properties

Seeting

Text Box
Label

name
name
Caption
name
Caption

txtinput
lblinput
Enter A String
cmdok
&OK

Command Button

14

Coding :Option Explicit


Private Sub cmdOk_Click()
'Variable Declaration
Dim strInput As String, strTemp As String
Dim intTotChar As Integer, intLength As Integer
'Fetch the string from text box (txtInput) into strInput
strInput = [Link]
'Get the length of string
intLength = Len(strInput)
'Clear the form contents if any
[Link]
For intTotChar = 1 To intLength
strTemp = Left(strInput, intTotChar)
[Link] strTemp
Next intTotChar
For intTotChar = intLength - 1 To 1 Step -1
strTemp = Left(strInput, intTotChar)
[Link] strTemp
Next intTotChar
End Sub

15

Assignment 11 :Write a program to calculate simple interest by using three labels and text box and
one command button.

Proprties :-

Control

Properties

Label

name
Caption
name
Caption
name
Caption

Label
Label
Label

Seeting
label1
&Principal Amount
label2
&interest Rate
label3
&Time Of Interest In
Year
label4
Simple Interest
lblresult
--------txtamont
---------txtrate
---------txttime
--------cmdinterest
&Simple Interest

name
Caption
name
Caption

Label
Textbox

name

Textbox

name

Textbox

name

Text
Text
Command Button

Text
name
Caption

16

Coding:-

Private Sub cmdinterest_Click()


[Link] = (Val([Link]) * Val([Link]) * _
Val([Link])) / 100
End Sub

17

Assignment 12 :Design a list box and add following items.


1. factorial
2. power
3. even number
4. sum of digit
write a function of particular items and show the output after selecting any item from
a list box.

Proprties :-

Control

Properties

Seeting

List Box

name
List

Label

name
Caption
name
Caption

list1
Factorial
Power
Even Number
Sum Of Digit
label2
result
lblresult
-----------

Label

18

Coding :Private Sub List1_Click()


If [Link] = 0 Then
Call fact
ElseIf [Link] = 1 Then
Call power
ElseIf [Link] = 2 Then
Call even
ElseIf [Link] = 3 Then
Call sum
End If
End Sub
Sub fact()
Dim x As Integer, i As Integer, y As Integer
x = InputBox("Enter The Number", "Value")
y=1
For i = x To 1 Step -1
y=y*i
Next
[Link] = y
End Sub
Sub power()
Dim x As Integer, y As Integer, z As Integer
x = InputBox("Enter The Number", "Value")
y = InputBox("Enter The Power", "Power")
z=1
For i = 1 To y
z=z*x
Next
[Link] = z
End Sub
Sub even()
Dim x As Integer, y As Integer
x = InputBox("Enter The Value", "Value")
If x Mod 2 = 0 Then
MsgBox "The Number Is Even", vbOKOnly, "Result"
Else
MsgBox " The Number Is Odd", vbOKOnly, "Result"
End If
End Sub
19

Sub sum()
Dim x As Integer, y As Integer, a As Integer
x = InputBox("Enter The First Value ", "Value")
y = InputBox("Enter The Second Value ", "Value")
a=x+y
[Link] = a
End Sub

20

Assignment 13:Write a procedure that will take a number as an argument and that will return number
is even or odd.

Proprties :-

Control

Properties

Seeting

Label

name
Caption
name
Caption

label1
Procedure
command 1
Take A Value

Command Button

Coding :Private Sub Command1_Click()


Dim a As Integer, res As Integer
a = InputBox(" Enter The Number ", "Value")
res = find(a)
If res = 0 Then
MsgBox "number is Even", vbInformation, "Even"
Else
MsgBox "number is Odd", vbInformation, "Odd"
End If
End Sub

21

Assignment 14 :Write a program to change image by using image box using timer.

Proprties :-

Control

Properties

Seeting

Image Box
Image Box
Image Box
Image Box
Command Button

name
name
name
name
name
Caption

imageScr1
imageScr 2
imageScr3
imageScr 4
cmdstartstop
&Start Rtation

22

Coding :Option Explicit


Dim strPath1 As String, strPath2 As String, strPath3 As String, strPath4 As
String
Private Sub Image1_Click()
End Sub
Private Sub cmdStartStop_Click()
Static blnState As Boolean
blnState = Not blnState
[Link] = blnState
If blnState Then
[Link] = "Stop Rotation"
Else
[Link] = "Start Rotation"
End If
End Sub
Private Sub Form_Load()
'Load all the image files from application path
strPath1 = [Link] & "\[Link]"
strPath2 = [Link] & "\[Link]"
strPath3 = [Link] & "\[Link]"
strPath4 = [Link] & "\[Link]"
'load the images
[Link] = LoadPicture(strPath1)
[Link] = LoadPicture(strPath2)
[Link] = LoadPicture(strPath3)
[Link] = LoadPicture(strPath4)
End Sub
Private Sub Timer1_Timer()
Static bytCounter As Byte
Select Case bytCounter
Case 0
[Link] = LoadPicture(strPath4)
[Link] = LoadPicture(strPath1)
[Link] = LoadPicture(strPath2)
[Link] = LoadPicture(strPath3)
bytCounter = 1
Case 1
[Link] = LoadPicture(strPath3)
[Link] = LoadPicture(strPath4)
[Link] = LoadPicture(strPath1)
[Link] = LoadPicture(strPath2)
bytCounter = 2
Case 2
23

[Link] = LoadPicture(strPath2)
[Link] = LoadPicture(strPath3)
[Link] = LoadPicture(strPath4)
[Link] = LoadPicture(strPath1)
bytCounter = 3
Case 3
[Link] = LoadPicture(strPath1)
[Link] = LoadPicture(strPath2)
[Link] = LoadPicture(strPath3) [Link] = LoadPicture(strPath4)
bytCounter = 0
End Select
End Sub

24

Assignment 15 :Design four checkbox any color i.e. selected by clicking on checkbox is add to list
box control.

Proprties :-

Control

Properties

Seeting

Check Box

name
Caption
name
Caption
name
Caption
name
Caption
name

chkred(0)
Red
chkred(1)
Green
chkred(2
Blue
chkred(3)
Yellow
list1

Check Box
Check Box
Check Box
List Box

Coding :Private Sub chkRed_Click(Index As Integer)


If chkRed(Index).Value = 1 Then
25

[Link] chkRed(Index).Caption
Else
Dim a As Integer
For a = 0 To [Link] - 1
If [Link](a) = chkRed(Index).Caption Then
[Link] a
Exit For
End If
End If
End Sub

26

Assigment 16 :Draw a form to enter phone number with city code, extract city and phone number in
two different textbox controls.

Proprties :-

Control

Properties

Seeting

Label

name
Caption
name
Caption
name
Caption
name
Text
name
Caption
name
Text
name
Caption

label1
Phone Number
label2
STD code
label3
Phone Number
text1
-------txtpin
-------txtphone
---------command1
&Show

Label
Label
Text Box
Text Box
Text Box
Command Button

27

Coding :Private Sub Command1_Click()


[Link] = Left([Link], 3)
[Link] = Right([Link], 7)
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
[Link] = Left([Link], 4)
[Link] = Right([Link], 7)
End If
End Sub
Private Sub mnuexit_Click()
Unload Form1
Unload MDIForm1
End Sub
Private Sub mnuform_Click()
[Link] 1
End Sub
Private Sub mnuGreen_Click()
[Link] = vbGreen
End Sub
Private Sub mnuRed_Click()
[Link] = vbRed
End Sub
Private Sub mnuYellow_Click()
[Link] = vbYellow
End Sub

28

Assigment 17 :Designe a checkbox any item like PEN 2/-, PENCILE 2/- i.e. selected in clicking on
checkbox and calculate their amount after input their quantity in textbox
.

Proprties :-

Control

Properties

Seeting

Label

name
Caption
name
Text
name
Caption
name
Caption
name
Caption
name
Caption
name
Caption
name
Caption

label1
Quantity
txtquantity
---------chkpencil
Pencil
chkpen
Pen
chkcutter
cutter
chkrubber
rubber
cmdamount
Amount
lblresult
----------

Text box
Check Box
Check Box
Check Box
Check Box
Command Button
Label

29

Coding :Private Sub cmdamount_Click()


Dim a As Integer
a = Val([Link])
If [Link] = 1 Then
[Link] = a * 2
lseIf [Link] = 1 Then
[Link] = a * 3
lseIf [Link] = 1 Then
[Link] = a * 4
lseIf [Link] = 1 Then
[Link] = a * 5
End If
End Sub

30

Assignment 18:Design a form with menu item color in sub menu red,Green , Blue on click form back
color should be change.

Proprties :-

Control

Properties

Seeting

Coding :Private Sub Blue_Click()


[Link] = vbBlue
End Sub
Private Sub Green_Click()
[Link] = vbGreen
End Sub
Private Sub Red_Click()
[Link] = vbRed
End Sub

31

Assignment :- 19
Designe a form to enter two number by using interface (input box control) display the
number on form.

32

Proprties :-

Control

Properties

Seeting

Command Button

name
Caption
name
Caption

command 1
Show on Form
command 1
Input Number

Command Button

Coding :Dim a As Integer, b As Integer


Private Sub Command1_Click()
Print a
Print b
End Sub
Private Sub Command2_Click()
a = InputBox("Enter The First Number", "Number")
b = InputBox("Enter The Second Number", "Number")
End Sub

33

Assignment 20 :In question , store student detail in a database (MS-Access) using data control
operation be ADD,EDIT,DELETE, and SAVE.

Proprties :-

Control

Properties

Seeting

Label

name
Caption
name
Caption
name
Caption
name
Caption
name
Caption
name

lblname
Name
lblroll
Roll Number
lblclass
Class
lblage
Age
lbladdress
Address
txtname

Label
Label
Label
Label
Text Box

34

Text Box
Text Box
Text Box
Text Box
Command Button
Command Button
Command Button
Command Button
Command Button
Command Button
Command Button
Command Button
Command Button

Text
name
Text
name
Text
name
Text
name
Text
name
Caption
name
Caption
name
Caption
name
Caption
name
Caption
name
Caption
name
Caption
name
Caption
name
Caption

------txtroll
------txtClass
-------txtage
------txtaddress
------cmdfirst
<<
cmdprevious
<
cmdnext
>
cmdLast
>>
cmdadd
&ADD
cmdrem
&Remove
cmdmodify
&Modify
cmdsearch
&Search
cmdupdate
&Update

Coding :Private Sub cmdAdd_Click()


'Add new Record
[Link]
For Each ctrl In [Link]
If TypeOf ctrl Is TextBox Then
With ctrl
.Locked = False
.Text = ""
End With
End If
Next
[Link]
End Sub
Private Sub cmdFirst_Click()
'Move to the first record
[Link]
End Sub

35

Private Sub cmdLast_Click()


'Move to the last Record
[Link]
End Sub
Private Sub cmdModify_Click()
'Unlock all the text boxes on form
For Each ctrl In [Link]
If TypeOf ctrl Is TextBox Then
[Link] = False
End If
Next
[Link]
End Sub
Private Sub cmdNext_Click()
'Move to the next record
[Link]
'Check if end of file then move to the last record
If [Link] Then
[Link]
End If
End Sub
Private Sub cmdPrevious_Click()
'Move to the previous record
[Link]
'Check if begining of file then move to the first record
If [Link] Then
[Link]
End If
End Sub
Private Sub cmdRem_Click()
'Delete the current record
[Link]
[Link]
'Check if end of file then move to the last record
If [Link] Then
[Link]
End If
End Sub
Private Sub cmdSearch_Click()
Dim intRoll As Integer
36

'Ask for the roll number of student


intRoll = Val(InputBox("Enter the roll number of student do you want to
search", "Roll number", "0"))
'Move to the first record
[Link]
'Loop through all the records of database
Do While [Link] = False
'Check if desired record is found then exit loop
If [Link]("rollno") = intRoll Then
Exit Do
End If
[Link]
Loop
'if end of file then record is not found and move to the last record
If [Link] Then
MsgBox "Cannot find the specified record ", vbInformation, "No
such record"
[Link]
End If
End Sub
Private Sub cmdUpdate_Click()
'Update the record and lock all the text boxes of form
[Link]
For Each ctrl In [Link]
If TypeOf ctrl Is TextBox Then
[Link] = True
End If
Next ctrl
End Sub
Private Sub Form_Load()
'Connect to the database and bound all textboxes to that data control(data1)
[Link] = [Link] & "\[Link]"
[Link] = "student"
'Bound the controls
[Link] = "name"
[Link] = "class"
[Link] = "address"
[Link] = "age"
[Link] = "rollno"
End Sub
37

Assignment 21 :Designe a Calculator.

Proprties :-

Control

Properties

Seeting

Command Button

name
Caption
name
Caption
name
Caption
name
Caption
name
Caption
name
Caption
name

cmdclear
Clear
Plus
+
div
/
minus
times
*
equals
=
lblscreen

Caption

---------

Command Button
Command Button
Command Button
Command Button
Command Button
Label

38

Coding :Option Explicit


Dim operand1 As Double, operand2 As Double
Dim operator As String
Dim cleardisplay As Boolean
Private Sub cmdclear_Click()
[Link] = ""
End Sub
Private Sub digit_Click(Index As Integer)
If cleardisplay Then
[Link] = ""
cleardisplay = False
End If
[Link] = [Link] + digit(Index).Caption
End Sub
Private Sub div_Click()
operand1 = Val([Link])
operator = "/"
[Link] = ""
End Sub
Private Sub dotbutton_Click()
If InStr([Link], ".") Then
Exit Sub
Else
[Link] = [Link] + "."
End If
End Sub
Private Sub equals_Click()
Dim result As Double
operand2 = Val([Link])
If operator = "+" Then result = operand1 + operand2
If operator = "-" Then result = operand1 - operand2
If operator = "*" Then result = operand1 * operand2
If operator = "/" And operand2 <> "0" Then
result = operand1 / operand2
End If
[Link] = result
End Sub
Private Sub minus_Click()
operand1 = Val([Link])
operator = "-"
39

[Link] = ""
End Sub
Private Sub plus_Click()
operand1 = Val([Link])
operator = "+"
[Link] = ""
End Sub
Private Sub times_Click()
operand1 = Val([Link])
operator = "*"
[Link] = ""
End Sub

40

Common questions

Powered by AI

The program prompts the user for a number using an InputBox, then assigns the task of determining if the number is even or odd to a separate procedure using the modulus operator. It uses a message box to inform the user of the result .

The implementation involves a loop from 1 to 25, checking each number for evenness using the modulus operator `If i Mod 2 = 0`. If true, it prints the number, effectively listing all even numbers between 1 and 25 .

The program calculates the area of a circle using the formula `3.14 * radius^2`, where the radius is obtained from an InputBox. When the user enters a radius, it is assigned to a variable which is then used in the calculation upon pressing a command button .

The program uses a timer to cycle through images by sequentially setting each image box's picture to the next image in a predefined sequence. The user's interaction through the 'Start Rotation' button toggles the timer's enabled state, controlling the start and stop of the image rotation process .

When a checkbox is selected, the program adds the checkbox's caption to a list box. If deselected, it searches the list box for the caption and removes it using a loop that checks each item in the list box against the deselected checkbox's caption .

The program uses a loop to iterate from 1 to 10. For each iteration, it multiplies the input number by the iterator and adds the result to a list box using `lsttable.AddItem`. This effectively displays the multiplication table of the input number for the first ten multiples .

The login form implementation limits the user to three incorrect login attempts. If the user enters the wrong username or password three times, a message box informs the user of the exceeded attempt limit, and the application exits immediately .

The program calculates simple interest using the formula `Principal * Rate * Time / 100` and displays the result in a label. It takes inputs from three text boxes for the principal amount, interest rate, and time of interest .

The program reads an integer from a text box and uses a series of `If-ElseIf` statements to map numbers 1 through 7 to corresponding days of the week, displaying the result in a message box. If the number does not correspond to a valid day, it displays an 'Invalid Number' message .

The program changes the form's background color upon selecting a menu item labeled 'color' with sub-options for 'Red', 'Green', and 'Blue'. Clicking on any sub-option changes the form's color according to predefined values for each color option .

You might also like