0

I would like remove item just before because its shows me twice the same item when I click on, but here, item number -1 doesn't exists And I don't know why. How I can resolve this ? Thank you.

private void DEXTarget_CheckedChanged(object sender, EventArgs e)
        {
            Logs("DEX(TMAPI) Target Checked");
            listView1.Items.RemoveAt(-1);
            PS3.ChangeAPI(SelectAPI.TargetManager);
            Var.API = true;
        }

        private void CEXTarget_CheckedChanged(object sender, EventArgs e)
        {

            Logs("CEX(CCAPI) Target Checked");
            PS3.ChangeAPI(SelectAPI.ControlConsole);
            Var.API = false;
        }

Log:

private void Logs(string text)
        {
            Var.lst = this.listView1.Items.Add(DateTime.Now.ToString("dd/MM/yy HH:mm"));
            Var.lst.SubItems.Add(text);
        }
5
  • 5
    RemoveAt uses 0 based index. Commented Apr 21, 2014 at 14:53
  • no because my first item is : Logs("Opening Program"); Commented Apr 21, 2014 at 14:54
  • The first item is index 0, the second item is index 1. So RemoveAt(-1) isn't going to work, the index position -1 doesn't exist. Commented Apr 21, 2014 at 15:13
  • So? Items still count from 0 upward. -1 is an indicator for 'Item not found'. Commented Apr 21, 2014 at 15:13
  • ListViewItemCollection.RemoveAt will through ArgumentOutOfRangeException if index is less than 0. Commented Apr 21, 2014 at 15:28

1 Answer 1

1

ListView Items Index start from 0 and ends with Count-1.

I think you are looking for removing the last Item from the ListView

Try This:

listView1.Items.RemoveAt(listView1.Items.Count - 1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.