C#:如何修复此 ArgumentOutofRangeException?
让自己成为一个密码管理器,我在一段代码中遇到了一些问题。应该发生的情况是应用程序打开一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
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 incrementingindex
when adding accounts, but when removing them, it stays the same. So after removing an account, yourListView
has fewer items in it thanindex
suggests.The fix is simple. Get rid of
index
. It doesn't look like you are using it for much anyway. In yourforeach
loop change your use ofListView.Items.Insert
toListView.Items.Add
.