Question

I am working on a project (simple phone book) for personal use. I am having trouble deleting the last remaining item in my listview (listView1). Here you can take a look how it looks like:

enter image description here

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. 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.

Since it doesn't makes sense to upload the whole code here, you can take a closer look at it at this link.

How can I solve this issue?

Was it helpful?

Solution

This should work for removing items from the listview:

Person person = new Person();
try { person = FindPerson(listView1.SelectedItems[0].Text); } catch { return; }
if (listView1.SelectedItems.Count > 0)
{
try
{
if (listView1.SelectedItems.Count == 0) return;
foreach (ListViewItem eachItem in listView1.SelectedItems)
{
people.RemoveAll(x => x.Name == eachItem.Text);
listView1.Items[listView1.Items.Count - 1].Selected = true;
listView1.Items.Remove(eachItem);
}
}
catch { }
ClearAll();
ReadOnlyON();
}
else
{
MessageBox.Show("Nothing is selected!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);

OTHER TIPS

perhaps you can use the clear method to remove the last item.

ListView1.Clear();

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.clear(v=vs.110).aspx

You can loop through the list backwards and remove each item or use as suggested ListView1.Clear();

Just replace this line

 listView1.SelectedItems[0].Remove();

in Remove() with

foreach ( ListViewItem eachItem in listView1.SelectedItems)
{
    listView1.Items.Remove(eachItem);
}

SelectedItems[0].Remove(); does not work for last item.

This is the way I delete items:

void Rmv()
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this contact?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (dialogResult == DialogResult.Yes)
        {
            Remove();
        }
        else if (dialogResult == DialogResult.No)
        {
            return;
        }
    }
    void Remove()
    {
            if (listView1.SelectedItems.Count > 0)
            {
                Person person = new Person();
                person = FindPerson(listView1.SelectedItems[0].Text);
                people.RemoveAt(listView1.SelectedItems[0].Index);
                foreach (ListViewItem eachItem in listView1.SelectedItems)
                {
                    listView1.Items.Remove(eachItem);
                }
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
                dateTimePicker1.Value = DateTime.Now;
                if (listView1.SelectedItems.Count == 0)
                {
                    textBox1.ReadOnly = false;
                    textBox2.ReadOnly = false;
                    textBox3.ReadOnly = false;
                    textBox4.ReadOnly = false;
                    textBox5.ReadOnly = false;
                    textBox6.ReadOnly = false;
                    dateTimePicker1.Enabled = true;
                    UserCount();
                }
            }

            else
            {
                MessageBox.Show("Nothing is selected! ", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
    }


    void UserCount()
    {
        try
        {
            if ((listView1.Items.Count) == 0)
            {
                toolStripLabel1.Text = Convert.ToString(listView1.Items.Count) + "& contacts";
            }
            else if ((listView1.Items.Count) == 1)
            {
                toolStripLabel1.Text = Convert.ToString(listView1.Items.Count) + "& contact";
            }
            else
            {
                toolStripLabel1.Text = Convert.ToString(listView1.Items.Count) + "& contacts";
            }
        }
        catch { }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top