Monday, December 10, 2012

Design a project for implementing collections in visual Basic.


Dim book As New Collection
Dim k As String
Dim cnt As Integer
Dim i,avrg As Integer

Private Sub CmdAdd_Click()
Lstbook.AddItem Text2.Text & Space(5) & Text1.Text
book.Add Text1.Text, Text2.Text
MsgBox "Book added to the list"
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End Sub
Private Sub CmdCount_Click()
cnt = book.Count
MsgBox "Total no. of books are " & cnt
End Sub
Private Sub CmdItem_Click()
k = InputBox("Enter Key")
MsgBox "Price of " & k & " is " & book.Item(k)
End Sub
Private Sub CmdRmv_Click()
k = InputBox("Enter Key")
For i = 0 To Lstbook.ListCount
pos = InStr(1, Lstbook.List(i), k, 1)
If pos Then
book.Remove k
Lstbook.RemoveItem i
MsgBox "Item removed"
Else
MsgBox "item doesn't exists"
End If
Next
End Sub


Private Sub CmdAvrg_Click()
Sum = 0
For i = 1 To book.Count
Sum = Sum + book.Item(i)
Next
avrg = Sum / book.Count
MsgBox " average of book price is " & avrg
End Sub
Private Sub Text1_LostFocus()
If Not IsNumeric(Text1.Text) Then
MsgBox "plz enter correct book price"
Text1.Text = ""
Text1.SetFocus
End If
End Sub
Private Sub CmdExit_Click()
End
End Sub

 Output




Note:- please set some property of your control like caption or id corresponding to your control