C#: How to fix this ArgumentOutofRangeExeception?
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
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
.
精彩评论