Visual basic Programming please help! Write a program that lets the user display
ID: 3821324 • Letter: V
Question
Visual basic Programming please help!
Write a program that lets the user display and modify an address book containing names, e-mail addresses, and phone numbers. The program should contain a class named Address. The Address class should contain the following information about one person: name, e-mail address, phone, and comments. The application should also have a collection named address List, which stores a collection of Address objects. The main window, shown in Figure 12-14, displays the names from the address book in a list box. The user should be able to input new names and addresses, using a form similar to the one shown in Figure 12-15.Explanation / Answer
Hi, this program makes use of three forms: two that have been mentioned int he question and a third form to edit entries in the address book. This form looks exactly like the form to add names.
The collection of address objects is declared in a global module. This module also has a sub to remove entries from the collection.
Naming conventions used:-
All forms start with prefix 'frm'. All text-boxes start with 'txt', all buttons start with 'btn' and the listboxes start with 'lst'.
The code is as follows:-
'module for global variables, subs and functions
Public Module GlobalVariables 'declare all global variables here
Public AddrBook As Collection 'collection for address book entries
Public Sub RemoveEntry(ByVal str As String)
For i = 1 To AddrBook.Count 'find the address with Name=str
If AddrBook(i).name = str Then
AddrBook.Remove(str) 'remove the adress entry with Name=str
End If
Next
End Sub
End Module
'the class file
Public Class Address
Private Name As String
Private Email As String
Private Phone As String
Private Comments As String
Public Function getName() As String
getName = Name 'return Name
End Function
Public Sub AddName(ByVal str As String)
Name = str 'assign the name
End Sub
Public Function AddEmail(ByVal str As String) As Boolean
'first validate that the entered string is in email id format
Dim At As Integer
Dim oneDot As Integer
Dim twoDots As Integer
Dim isEmail As Boolean
isEmail = True
At = InStr(1, str, "@", vbTextCompare)
+ 2, str, ".", vbTextCompare)
twoDots = InStr(At + 2, str, "..", vbTextCompare)
If At = 0 Or Or Not twoDots = 0 Or Right(Email, 1) = "." Then isEmail = False
'assign the email id if the entered string is in correct format and return True
If isEmail = True Then
Email = str
AddEmail = True
Else 'else show error message and return false
MsgBox("Enter a valid Email ID.")
AddEmail = False
End If
End Function
Public Sub AddPhone(ByVal str As String)
Phone = str 'assign the phone number
End Sub
Public Sub AddComments(ByVal str As String)
Comments = str 'assign the comments
End Sub
End Class
'Email Address Book Form
Public Class frmAddrBook
Private Sub frmAddrBook_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each obj In AddrBook
lstNames.Items.Add(obj.getName)
Next
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit() 'close the program
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
frmEdit.Show()
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
RemoveEntry(lstNames.SelectedItem) 'call the remove entry sub
lstNames.Items.Remove(lstNames.SelectedItem)
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
frmEdit.Show()
End Sub
End Class
'Add Name form
Public Class frmAddNew
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Dim AddrEntry As New Address
AddrEntry.AddName(txtName.Text)
If AddrEntry.AddEmail(txtEmail.Text) = False Then
txtEmail.Text = "" 'if email format is wronf then clear the text box
Exit Sub
End If
AddrEntry.AddPhone(txtPhone.Text)
AddrEntry.AddComments(txtComments.Text)
AddrBook.Add(AddrEntry) 'add object to the collection
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close() 'close this form
End Sub
End Class
'added a new form for editing
Public Class frmEdit
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Dim AddrEntry As New Address
AddrEntry.AddName(txtName.Text)
If AddrEntry.AddEmail(txtEmail.Text) = False Then
txtEmail.Text = "" 'if email format is wronf then clear the text box
Exit Sub
End If
AddrEntry.AddPhone(txtPhone.Text)
AddrEntry.AddComments(txtComments.Text)
AddrBook.Add(AddrEntry) 'add object to the collection
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close() 'close this form
End Sub
Private Sub frmEdit_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To AddrBook.Count
If AddrBook(i).name = My.Forms.frmAddrBook.lstNames.SelectedItem Then
Exit For
End If
If i > 0 And i < AddrBook.Count Then 'display selected entry
txtName1.Text = AddrBook(i).name
txtEmail1.Text = AddrBook(i).email
txtPhone1.Text = AddrBook(i).phone
txtComments1.Text = AddrBook(i).comments
End If
Next
End Sub
End Class
Hope this helps!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.