简体   繁体   English

附加信息:InvalidArgument =值“ 0”对“索引”无效

[英]Additional information: InvalidArgument=Value of '0' is not valid for 'index'

I realize this may look like a duplicate question (I've seen many other questions asking about this error), however I cannot find an answer which explains the issue I'm having. 我意识到这看起来像是一个重复的问题(我已经看到很多其他问题都在问这个错误),但是我找不到答案来解释我遇到的问题。 The error is provoked by a call to 该错误是由调用引起的

invList.SelectedItems[0].Text //invList is a ListView

From what I've read, the error when provoked by this instruction is indicative of attempted access to an empty list (or array?) of selected items in the ListView. 据我所读,此指令引起的错误表示试图访问ListView中选定项目的空列表(或数组?)。 However, the ListView I am attempting to access is populated with items at the time of the instruction's execution. 但是,我正在尝试访问的ListView在执行指令时填充有项目。 Here are the methods in which I issue this instruction: 以下是发出此指令的方法:

Method #1: deleteItem 方法1:deleteItem

public bool deleteItem()
    {
        if (invList.SelectedItems.Count == 0) //if no item selected...
        {
            MessageBox.Show("Error: No item selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return false;
        }
        else
        {
            DialogResult dialogResult = MessageBox.Show(
                "Are you sure you want to delete item: " + invList.SelectedItems[0].Text + "?",
                "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dialogResult == DialogResult.Yes)
            {
                for (int i = 0; i < itemRecords.Count; i++)
                {
                    if ((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == clinicName))
                    {
                        itemRecords.Remove(itemRecords[i]);
                        listRefresh();
                    }
                }
                return true; //return true to indicate that data has been edited
            }
            return false; // return false to indicate that nothing has changed
        }
    }

Method #2: updateItem 方法2:updateItem

 public bool updateItem()
    {
        if (invList.SelectedItems.Count == 0) //if no item selected
        {
            MessageBox.Show("Error: No item selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return false;
        }
        else
        {
            for (int i = 0; i < itemRecords.Count; i++)
            {
                //if id == the id of the selected item
                if((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == this.Text))
                {
                    ItemAddition itemAddition = new ItemAddition(itemRecords, itemRecords[i], this);
                    itemAddition.ShowDialog();
                }
            }
            return true;
        }
    }

listRefresh: listRefresh:

       public void listRefresh()
    {
        invList.Items.Clear();
        loadItems();
    }

loadItems: loadItems:

        private void loadItems()
    {
        foreach (Record r in itemRecords)
        {
            if (r.practice == clinicName)
                invList.Items.Add(r.ToString());
        }
    }

The error is invoked when the first method is called, but NOT when the second method is called. 调用第一个方法时将调用该错误,但调用第二个方法时将不调用该错误。 This inconsistency is the reason for my confusion. 这种不一致是我感到困惑的原因。 Is there some reason this error would only occur in the first method? 是否由于某些原因仅在第一种方法中会发生此错误?

Move refreshing method after remove items. 删除项目后移动刷新方法。 If you move item and rebind, then you will lost selection. 如果您移动项目并重新绑定,那么您将丢失选择。

   for (int i = 0; i < itemRecords.Count; i++)
    {
        if ((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == clinicName))
        {
            itemRecords.Remove(itemRecords[i]);
            //listRefresh();
        }
    }

listRefresh();                
return true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM