private void deletebtn_Click(object sender, EventArgs e)
{
addressbook[lstContacts.SelectedIndex].firstnamestring = "";
addressbook[lstContacts.SelectedIndex].lastnamestring = "";
addressbook[lstContacts.SelectedIndex].addressstring = "";
addressbook[lstContacts.SelectedIndex].citystring = "";
addressbook[lstContacts.SelectedIndex].statestring= "";
addressbook[lstContacts.SelectedIndex].zipcodestring = "";
firstname.Text = "Add a new Contact";
lastname.Text = "";
streetaddy.Text = "";
city.Text = "";
state.Text = "";
zipcode.Text = "";
int listselction = lstContacts.SelectedIndex;
string removed = "Remove Contact";
lstContacts.Items.Insert(listselction, removed);
lstContacts.Items.RemoveAt(listselction + 1);
lstContacts.ClearSelected();
lstContacts.Refresh();
}
答案 0 :(得分:0)
在第
行设置断点lstContacts.Items.RemoveAt(listselction + 1);
并查看listselction
的值。然后检查.Items
是否包含这么多项目。请记住,索引从0
开始。你可能试图删除索引大于或等于'.Items.Count - 1`。