How about this:
Ok, so that seems to work fine. I was trying to code the "Delete" button which will delete the entry in the array that corresponds to the name selected in the listbox. I tried to do this with a for loop seen below, but I am getting a lot of errors. Should I be doing this a different way? This works fine in another program I have that uses an array rather than an array of structures.Code:public struct Contacts { public string FirstName; public string LastName; public string Street; public string City; public string State; public string Zip; } Contacts [] arrayOfContacts = new Contacts [20]; int index = 0; private void btnExit_Click(object sender, EventArgs e) { // Close program this.Close(); } private void btnAdd_Click(object sender, EventArgs e) { // Declare variables Contacts entry; entry.FirstName = tbxFirstName.Text; entry.LastName = tbxLastName.Text; entry.Street = tbxStreet.Text; entry.City = tbxCity.Text; entry.State = tbxState.Text; entry.Zip = tbxZip.Text; // Add contact to array if (index < 20) { arrayOfContacts[index++] = entry; } else { MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full"); } // Display Contacts (First and Last name only) in Listbox lstContacts.Items.Add(entry.LastName + ", " + entry.FirstName); // Return message when Array reaches maximum of 20 entries // Clear Text Boxes tbxFirstName.Text = ""; tbxLastName.Text = ""; tbxStreet.Text = ""; tbxCity.Text = ""; tbxState.Text = ""; tbxZip.Text = ""; } } }
Code:private void btnDelete_Click(object sender, EventArgs e) { for (int k = 0; k < arrayOfContacts.Length; k++) try { if (arrayOfContacts[k] = lstContacts.SelectedItem.ToString()) { arrayOfContacts[k] = null; } } catch { } } //Display Refreshed Records in ListBox lstContacts.Items.Clear(); for (int j = 0; j < index; j++) { if (arrayOfContacts[j] != null) { lstContacts.Items.Add(arrayOfContacts[j].ToString().Trim()); }




Reply With Quote