I can not fix this error. It is in red below. All my forms are listed on the scr
ID: 3916475 • Letter: I
Question
I can not fix this error. It is in red below. All my forms are listed on the screenshot. I need it to open and add a new contact in a separate form but the code breaks as soon as I hit the add button.
Here is formAddressBook.vb code
Imports AddNew.address
Public Class formAddressBook
Public AddressList As New Collection
'The updateListBox procedure updates the contents of the list box.
Private Sub UpdateListBox()
'Clear the listbox.
ListBxDisplay1.Items.Clear()
'Load the ID numbers in the collection into the list box.
Dim a As AddNew.address.Address
For Each a In AddressList
ListBxDisplay1.Items.Add(a.Name)
Next
'select the first item in the list.
If ListBxDisplay1.Items.Count > 0 Then
ListBxDisplay1.SelectedIndex = 0
End If
End Sub
Private Sub ButAdd_Click(sender As Object, e As EventArgs) Handles ButAdd.Click
'Create an instance of the AddNew form.
Dim frmAddNew As New AddNew
AddNew.Show()
frmAddNew.ShowDialog()
Try
AddressList.Add(frmAddNew.objStudent, frmAddNew.objStudent.Name)
Catch ex As Exception
End Try
UpdateListBox()
End Sub
'Remove button function.
Private Sub ButRemove_Click(sender As Object, e As EventArgs) Handles ButRemove.Click
Dim intIndex As Integer
'Mak sure an item is selected.
If ListBxDisplay1.SelectedIndex <> -1 Then
'confirm that the user wants to remove the item.
If MessageBox.Show("Are You sure?", "Confirm Deletion", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
'Rtrive the student's data from the collection.
intIndex = ListBxDisplay1.SelectedIndex
Try
'Remove the selected item from the collection.
AddressList.Remove(ListBxDisplay1.SelectedItem.ToString())
'update the list box.
UpdateListBox()
Catch ex As Exception
'Error message.
MessageBox.Show(ex.Message)
End Try
End If
End If
End Sub
Private Sub ButEdit_Click(sender As Object, e As EventArgs) Handles ButEdit.Click
If ListBxDisplay1.SelectedIndex <> -1 Then
Dim st As New Address
st = AddressList.Item(ListBxDisplay1.SelectedItem.ToString())
frmAddNew.txtName.Text = st.Name
frmAddNew.txtMail.Text = st.EMail
frmAddNew.txtPhone.Text = st.Phone
frmAddNew.rtbNotes.Text = st.Comments
frmAddNew.ShowDialog()
AddressList.Remove(st.Name)
AddressList.Add(frmAddNew.objStudent, frmAddNew.objStudent.Name)
UpdateListBox()
End If
End Sub
Private Sub ButExit_Click(sender As Object, e As EventArgs) Handles ButExit.Click
Application.Exit()
End Sub
End Class
Here is AddNew.vb code
Public Class AddNew
Public objStudent As New address
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButOkAddNew.Click
'Get data from the form into the object.
objStudent.Name = TxtBxNameAddNew.Text
objStudent.Phone = TxtBxPhoneAddNew.Text
objStudent.EMail = TxtBxEmailAddressAddNew.Text
objStudent.Comments = ListBxAddNew.Text
ClearForm()
Me.Close()
End Sub
'The clearForm procedure clears the form.
Public Sub ClearForm()
'Clear the text boxes.
TxtBxEmailAddressAddNew.Clear()
TxtBxNameAddNew.Clear()
TxtBxPhoneAddNew.Clear()
ListBxAddNew.ClearSelected()
'Reset the focus.
TxtBxNameAddNew.Focus()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButCloseAddNew.Click
'Create an instance of the Main Form form.
Me.Close()
End Sub
End Class
Here is address.vb code
Public Class address
Public Property Name As String
Public Property Phone As String
Public Property EMail As String
Public Property Comments As String
Public Class Address
Private personName As String
Private personEmail As String
Private personPhone As String
Private personComments As String
Public Property Name As String
Set(ByVal value As String)
personName = value
End Set
Get
Return personName
End Get
End Property
Public Property EMail As String
Set(ByVal value As String)
personEmail = value
End Set
Get
Return personEmail
End Get
End Property
Public Property Phone As String
Set(ByVal value As String)
personPhone = value
End Set
Get
Return personPhone
End Get
End Property
Public Property Comments As String
Set(ByVal value As String)
personComments = value
End Set
Get
Return personComments
End Get
End Property
End Class
End Class
Explanation / Answer
Public Class Form1
Private objContact As New ArrayList
Private Sub BtnAddContact_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddContact.Click
Dim objcontact(100) As Contact
Dim objNewContact As New Contact
objNewContact.firstName = InputBox("Enter First Name")
objNewContact.lastName = InputBox("Enter Last Name")
objNewContact.telNo = InputBox("Enter Telephone Number")
objNewContact.address = InputBox("Enter Address")
objNewContact.email = InputBox("Enter Email Address")
objNewContact.webAddress = InputBox("Enter Web Address")
LstContacts.Items.Add(objNewContact)
End Sub
Private Sub LstContacts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstContacts.SelectedIndexChanged
If LstContacts.SelectedItems.Count > 0 Then MessageBox.Show(LstContacts.SelectedItem.ToString())
End Sub
Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Class
Public Class Contact
Public firstName As string
Public lastName As String
Public address As String
Public telNo As Long
Public email As String
Public webAddress As String
Private contacts As ArrayList
Sub main()
contacts = New ArrayList
Console.WriteLine("New Contact Details", contacts.Count)
Console.ReadLine()
End Sub
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.