I can not fix this error. It is in red below. All my forms are listed on the scr
ID: 3916503 • 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
I can see inconsistencies in the code provide by you and the code in the screenshot. In the code provided, you are using Dim frmAddNew As New AddNew whereas in the screenshot, it shows Dim AddNew As New frmAddNew.
Basically, you are trying to create an instance of the class AddNew called 'frmAddNew'. It should be done using the following statement shown below.
Dim frmAddNew As New AddNew()
After this, you should proceed with your program with the exisitng functionalitites. In case you get any other errors, do let me know.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.