private void deleteAttendeeButton_Click(object send, EventArgs e) { // get # in attendeeGroupBox# where # starts at 1 Control btn = (Control)send; string index = btn.Name.Substring(btn.Name.Length - 1, 1); DeleteAttendee = true; numOfAttendees--; // delete input from panel foreach (Control item in attendeePanel.Controls) { if (item.Name == "attendeeGroupBox" + index) { attendeePanel.Controls.Remove(item); } } // reposition groupboxes RepositionAttendees(); // get attendee IDs to delete from database int attIndex = Attendees.FindIndex(att => att.Name == "attendeeEmailTextbox" + index); int rsvpIndex = AttendeesRsvp.FindIndex(rsvp => rsvp.Name == "rsvpComboBox" + index); List <int> dbAttendeesId = dbIndex != -1 ? db.GetEvents()[dbIndex].attendeesId : null; if (updateClicked && (attIndex != -1) && (dbAttendeesId != null) && !(attIndex >= dbAttendeesId.Count)) { AttendeesID.Add(dbAttendeesId[attIndex]); } else { Attendees.RemoveAt(attIndex); AttendeesRsvp.RemoveAt(attIndex); } }