C#:如何修复此 ArgumentOutofRangeException?

发布于 2024-11-09 05:08:37 字数 2285 浏览 4 评论 0原文

让自己成为一个密码管理器,我在一段代码中遇到了一些问题。应该发生的情况是应用程序打开一个 xml 文件,然后使用该 xml 文档(帐户)中包含的项目填充列表视图。右键单击列表视图会提供各种选项的上下文菜单,所有这些选项单独工作都很好。但是,打开 xml 文档后,然后从列表视图中删除一个帐户,然后尝试添加另一个帐户,它会抛出以下内容:

 ArgumentOutOfRangeException unhandled.
 InvalidArgument=Value of '4' is not valid for 'index'.
 Parameter name: index

我假设出了问题是当我从列表视图中删除该帐户时,我'我弄乱了索引变量的计数,该变量在应用程序启动时为 xml 文档中的每个项目递增。不确定在不破坏代码其他部分的情况下修复该问题的最佳方法。我正在考虑在删除帐户后通过计算 listView 中现在的总项目数来重置“index”的值,但不确定这是否最好。这是打开 xml 时代码的样子。

private void openPasswordFileToolStripMenuItem_Click(object sender, EventArgs e)
    {
        System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();

        try
        {
            loadDoc.Load(Application.StartupPath + "\\database.xml");
        }
        catch (System.IO.FileNotFoundException)
        {
            MessageBox.Show("Password Database does not exist!");
        }
        foreach (System.Xml.XmlNode node in loadDoc.SelectNodes("/Database/Account"))
        {
            lvItem = listView1.Items.Insert(index, node.Attributes["Description"].InnerText); ;
            lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem, node.Attributes["Username"].InnerText)); ;
            lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem, node.Attributes["Password"].InnerText)); ;
            index += 1;
        }
    }

最后是删除帐户的部分:

 private void removeSelectedAccountToolStripMenuItem_Click(object sender, EventArgs e)
    {
        listView1.Items.Remove(listView1.SelectedItems[0]);
    }

再次一切正常,直到执行以下序列:帐户文件打开 -->帐户已删除 -->添加了另一个帐户。此时会引发异常,并且新帐户永远不会添加到列表视图中。

以下是异常详细信息。这是“堆栈转储”?

  System.ArgumentOutOfRangeException was unhandled
  Message=InvalidArgument=Value of '3' is not valid for 'index'.
Parameter name: index
  Source=System.Windows.Forms
  ParamName=index
  StackTrace:
       at System.Windows.Forms.ListView.ListViewItemCollection.Insert(Int32 index, ListViewItem item)
       at System.Windows.Forms.ListView.ListViewItemCollection.Insert(Int32 index, String text)
       at PassKeeper.Form1.addAccountToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Hamann\documents\visual studio 2010\Projects\PassMan\PassMan\Form1.cs:line 35

Making myself a password manager and I'm running into some problems with a segment of code. Whats supposed to happen is the application opens an xml file, and then populates a listview with the items contained in that xml document (the accounts). Right clicking on the listview gives a context menu for various options, all of which work fine individually. However, after opening the xml document, and then remove one of the accounts from the listview, then attempt to add another account, it throws the following:

 ArgumentOutOfRangeException unhandled.
 InvalidArgument=Value of '4' is not valid for 'index'.
 Parameter name: index

I'm assuming whats going wrong is when I remove the account from the listview, I'm messing up the count of the index variable which increments for every item in the xml document on application start. Not sure the best way to go about fixing that without breaking other portions of code. I was thinking of resetting the value of 'index' after removing an account by counting how many total items are now in the listView but not sure if thats best. Here's what the code looks like when the xml is opened.

private void openPasswordFileToolStripMenuItem_Click(object sender, EventArgs e)
    {
        System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();

        try
        {
            loadDoc.Load(Application.StartupPath + "\\database.xml");
        }
        catch (System.IO.FileNotFoundException)
        {
            MessageBox.Show("Password Database does not exist!");
        }
        foreach (System.Xml.XmlNode node in loadDoc.SelectNodes("/Database/Account"))
        {
            lvItem = listView1.Items.Insert(index, node.Attributes["Description"].InnerText); ;
            lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem, node.Attributes["Username"].InnerText)); ;
            lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem, node.Attributes["Password"].InnerText)); ;
            index += 1;
        }
    }

And finally the segment for removing the account:

 private void removeSelectedAccountToolStripMenuItem_Click(object sender, EventArgs e)
    {
        listView1.Items.Remove(listView1.SelectedItems[0]);
    }

Again everything works fine until the following sequence is executed: Accounts File Opened --> Account Removed --> Another Account added. At which point the exception is thrown and the new account is never added to the list view.

Here are the exception details. This is the 'stack dump'?

  System.ArgumentOutOfRangeException was unhandled
  Message=InvalidArgument=Value of '3' is not valid for 'index'.
Parameter name: index
  Source=System.Windows.Forms
  ParamName=index
  StackTrace:
       at System.Windows.Forms.ListView.ListViewItemCollection.Insert(Int32 index, ListViewItem item)
       at System.Windows.Forms.ListView.ListViewItemCollection.Insert(Int32 index, String text)
       at PassKeeper.Form1.addAccountToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Hamann\documents\visual studio 2010\Projects\PassMan\PassMan\Form1.cs:line 35

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2024-11-16 05:08:37

由于 index 没有在我看到的任何方法中声明,因此我假设它是一个类成员。添加帐户时,您总是会递增 index,但在删除帐户时,它保持不变。因此,删除帐户后,您的 ListView 中的项目少于 index 建议的项目。

修复方法很简单。摆脱索引。无论如何,看起来你并没有经常使用它。在 foreach 循环中,将 ListView.Items.Insert 的使用更改为 ListView.Items.Add

Since index is not declared in any method I saw, I assume that it is a class member. You are always incrementing index when adding accounts, but when removing them, it stays the same. So after removing an account, your ListView has fewer items in it than index suggests.

The fix is simple. Get rid of index. It doesn't look like you are using it for much anyway. In your foreach loop change your use of ListView.Items.Insert to ListView.Items.Add.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文