I am working on a project (simple phone book) for personal use. Basically, the contacts are displayed in a listview and stored in a XML file. I am having trouble deleting the last remaining item in my listview (listView1).

So, let's say that I have got 5 contacts in the list and when I try to remove all of them, it's not possible. It is possible to remove only 4 of them. When I try to remove all of them and then close/run the application, there will be no removed contacts - all of them will still be there, like I haven't removed them at all. When I try to remove 4 of them and I close/run the program, they would be deleted. When I try to remove the last one, it is not possible either - when I close/run app it would always remain there.

Also, sometimes there is an error as well, when I try to just select some of the contacts - usually after already deleting them. So, just to make things clear, let's say that I have got 10 contacts and remove 5 of them, when I try to select the sixth contact - the error shows:

IMAGE

I think maybe this part of the code is bugged:

private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                toolStripEdit.Enabled = false;
                RefreshAll();
                return;
            }
            Person person = new Person();
            person = FindPerson(listView1.SelectedItems[0].Text);
            txt_Name.Text = person.Name;
            txt_City.Text = person.Hometown;
            txt_Address.Text = person.Address;
            txt_Phone.Text = person.Phone;
            txt_Mail.Text = person.Email;
            txt_MoreInfo.Text = person.AdditionalInfo;
            dateTimePicker1.Value = person.Birthday;
            ReadOnlyON();
            toolStripEdit.Enabled = true;
            ExpandThis();
        }

and this one as well:

void Rmv()
        {
            if (Properties.Settings.Default.Remove == true)
            {
                DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this contact?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    Remove();
                    if (Properties.Settings.Default.Sync == true) { Sync(); }
                }
                else if (dialogResult == DialogResult.No)
                {
                    return;
                }
            }
            else
            {
                Remove();
                if (Properties.Settings.Default.Sync == true) { Sync(); }
            }
        }

void Remove()
        {
            Person person = new Person();
            person = FindPerson(listView1.SelectedItems[0].Text);
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    if (listView1.SelectedItems.Count == 0) return;
                    people.RemoveAt(listView1.SelectedItems[0].Index);
                    foreach (ListViewItem eachItem in listView1.SelectedItems)
                    {
                        listView1.Items.Remove(eachItem);
                    }
                }
                catch { }
                ClearAll();
                ReadOnlyON();
            }
            else
            {
                MessageBox.Show("Nothing is selected!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            ReadOnlyOFF();
            UserCount();
            if (Properties.Settings.Default.Sync == true) { Sync(); }
        }

Since it doesn't makes sense to upload the whole code here, I have uploaded it here if someone wants to help and take a closer look at it.

Thanks in advance.

Edited 3 Years Ago by Exinta

Could you be more specific, please? I am quite new to c# and I completely do not know what did you mean by that. Thanks in advance.

It's missing from the code at the link you posted. It's hard to run your code without that class since much of your code relies on it.

This article has been dead for over six months. Start a new discussion instead.