This is a homework assignment for Visual Basic (using Visual Studio 2013). This is my first question on this site (or any site).
I am trying to make a phone directory that utilizes lists. The directory should be able to search, add and delete contacts.
On my form, I have a list box that has names already entered through the collection property. I have a label that outputs the corresponding phone number. I have added 10 names and 10 phone numbers. (phone numbers were added with a list - at design time)
At run time, the user should be able to click the "add contact" button to add another contact to the list. I am using two input boxes; first prompts for the name to add and the second prompts for the phone number.
First contact that I try to add, seems to be working out just fine. My issue is when I try to add a second contact to the list, the phone number from the first add changes to reflect the same number that is being added for the second add. (looks like second contact phone number is overriding what I put for the first add) Just to be clear, when the program runs, I start out with 10 names in a list box and 10 numbers that can only be viewed if you click on the name in the list box. When I added the first one, it made the count 11 and then adding the second one, the count went to 12. The phone numbers are being added to a list (not list box).
I do not receive any error messages, just the information is not getting put into the list correctly (the list that is similar to an array).
Here is the code I have:
Let me know if I need to be more clear on what the issue is.
FYI: I just tested it again before submitting my question. I am just adding a name and a one digit number just to test it. When I enter first added contact, I used the number 5. Added second contact, I used the number 6. When I check at that point, the first contact showed a 6 and the second contact showed a 6. However, I tested doing 3 added contacts. Same number for the first add (5), same number for the second add (6) and used 7 for the third added contact. When I checked the results, first added contact changed to 7, second contact stayed at 6 and third contact shows 7. I have no idea what is going on??
Option Explicit On
Option Strict On
'=========== Class mainForm =================
Public Class mainForm
Const MAX_SUBSCRIPT_Integer As Integer = 9
Dim inputNameString As String 'user contact name input
Dim inputPhoneString As String 'user phone number input
Private phoneList As New List(Of String)
'============== mainForm_Load ====================
Private Sub mainForm_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
splashForm.ShowDialog()
phoneList.Add("555 - 266 - 9563")
phoneList.Add("555 - 266 - 5461")
phoneList.Add("555 - 266 - 7412")
phoneList.Add("555 - 266 - 5642")
phoneList.Add("555 - 266 - 6721")
phoneList.Add("555 - 266 - 1465")
phoneList.Add("555 - 266 - 3541")
phoneList.Add("555 - 266 - 2874")
phoneList.Add("555 - 266 - 9114")
phoneList.Add("555 - 266 - 2245")
End Sub
'======= namesListBox_SelectedIndexChanged() =====================
Private Sub namesListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles namesListBox.SelectedIndexChanged
If namesListBox.SelectedIndex >= 0 Then
phoneNumberLabel.Text = (phoneList(CInt(namesListBox.SelectedIndex.ToString())))
Dim count As Integer
contactPictureBox.Image = contactImageList.Images(namesListBox.SelectedIndex)
count += 1
End If
End Sub
'============== addButton_Click =====================
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
inputNameString = (InputBox("Enter name of Contact. Ex. Doctor: Dr. Sigmund Freud ", "Add Contact"))
inputPhoneString = (InputBox("Enter contact phone number. Ex. 555-555-1212", "Add Contact Phone Number"))
namesListBox.Items.Add(inputNameString.ToString())
phoneList.Add(inputPhoneString.ToString())
AddElementToPhoneString(inputPhoneString)
End Sub
'============== AddElementToPhoneString() ========================
Public Sub AddElementToPhoneString(ByVal stringToAdd As String)
phoneList(MAX_SUBSCRIPT_Integer + 1) = stringToAdd
Dim countInteger As Integer
countInteger = phoneList.Count()
countLabel.Text = CStr(CInt(phoneList.Count))
End Sub
End Class