|
-
March 13th, 2008, 02:18 AM
#1
ListView.Items.Clear() Exception
Hello
I got that exception while using: ListView.Items.Clear()
((An Unhandled exception of type 'System.ArgumentOutOfRangeException' occured in system.windows.forms.dll
Additional Information: Specified argument was out of the range of valid values.))
i tried to remove only one item but there was no problem either it's not the last one.
Can u help me
Thanks
-
March 13th, 2008, 03:37 AM
#2
Re: ListView.Items.Clear() Exception
If you can show us your code then it will be very helpfull for the people out here to understand it....
-
March 13th, 2008, 04:11 AM
#3
Re: ListView.Items.Clear() Exception
ok, i'm adding a simple error logger to an application which shows the errors to the user and allows him to clear an error or clear all the errors by clicking (right click) on the listview (lvErrors).
here is the part of the code related to the error logger:
Code:
private System.Windows.Forms.ContextMenu cmClrErrors;
private System.Windows.Forms.MenuItem clrAllErrorsMenuItem;
private System.Windows.Forms.MenuItem clrErrorMenuItem;
public static System.Windows.Forms.ListView lvErrors;
Content cError;
..
public MainForm()
{
cError = _manager.Contents.Add(lvErrors , "Errors", _internalImages, _count % 6);
lvErrors.MouseDown+=new MouseEventHandler(lvErrors_MouseDown);
}
private void LoadManagerDefaults()
{
DefineContentState(cError);
DefineControlColors(cError);
_manager.AddContentWithState(cError, State.DockBottom);
}
private void InitializeComponent()
{
this.ErrMsgType = new System.Windows.Forms.ColumnHeader();
this.ErrMsg = new System.Windows.Forms.ColumnHeader();
MainForm.lvErrors = new System.Windows.Forms.ListView();
this.cmClrErrors = new ContextMenu();
this.clrAllErrorsMenuItem = new MenuItem();
this.clrErrorMenuItem = new MenuItem();
MainForm.lvErrors.AllowColumnReorder = true;
MainForm.lvErrors.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.ErrMsgType, this.ErrMsg});
this.cmClrErrors.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.clrAllErrorsMenuItem, this.clrErrorMenuItem});
this.clrAllErrorsMenuItem.Text = "Clear All";
this.clrAllErrorsMenuItem.Click+=new EventHandler(clrAllErrorsMenuItem_Click);
this.clrErrorMenuItem.Text = "Clear";
this.clrErrorMenuItem.Click+=new EventHandler(clrErrorMenuItem_Click);
}
protected void lvErrors_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
cmClrErrors.Show(lvErrors, new Point(e.X,e.Y));
return;
}
}
private void clrAllErrorsMenuItem_Click(object sender, System.EventArgs e)
{
lvErrors.Items.Clear();
}
private void clrErrorMenuItem_Click(object sender, System.EventArgs e)
{
int selCount = lvErrors.SelectedItems.Count;
for(int i=0; i<selCount;)
{
lvErrors.SelectedItems[i].Remove();
selCount --;
}
}
//this is the method wich is called to add an element to the List view lvErrors
public static void AddErrorMessageToListView(string errMsg, ErrorLogerTypes type)
{
string prefix = "";
int imageIndx = 0;
switch(type)
{
case ErrorLogerTypes.Error:
prefix = "Error";
imageIndx = 0;
break;
case ErrorLogerTypes.Warning:
prefix = "Warning";
imageIndx = 1;
break;
case ErrorLogerTypes.Info:
prefix = "Info";
imageIndx = 2;
break;
case ErrorLogerTypes.Debug:
prefix = "Fatal";
imageIndx = 3;
break;
}
MainForm.lvErrors.SmallImageList = ig;
// Create a ListViewItem object for evey item that you wish to add the ListView.
string[] lv = new String[2];
lv[0] = prefix;
lv[1] = errMsg;
MainForm.lvErrors.Items.Add(new ListViewItem(lv,imageIndx));
}
Thanks
Last edited by cjard; March 14th, 2008 at 03:55 AM.
Reason: use code tags: http://www.codeguru.com/forum/misc.php?do=bbcode
-
March 13th, 2008, 08:38 AM
#4
Re: ListView.Items.Clear() Exception
Can you post the actual exception as well? Also can you use the code tags next time? It will make the code a lot easier to read. The only thing that seems odd to me is the fact that you are removing from the list view item through the SelectedItems collection.
Here's what MSDN says about the ListViewItem.Remove method:
Removes the item from its associated ListView control.
So there is likely to be a fallacy in your loop construct because you are always indexing SelectedItems[0] which may not be correct. It would easier to diagnose the problem with all the exception text.
-
March 13th, 2008, 09:19 AM
#5
Re: ListView.Items.Clear() Exception
Is this the code that's throwing the error?
Code:
private void clrErrorMenuItem_Click(object sender, System.EventArgs e)
{
int selCount = lvErrors.SelectedItems.Count;
for(int i=0; i<selCount;)
{
lvErrors.SelectedItems[i].Remove();
selCount --;
}
}
It should be
Code:
private void clrErrorMenuItem_Click(object sender, System.EventArgs e)
{
for(int i=lvErrors.SelectedItems.Count-1; i>=0; i--)
{
lvErrors.SelectedItems[i].Remove();
}
}
I've never seen anyone do it the way you are trying to there.
-
March 13th, 2008, 09:34 AM
#6
Re: ListView.Items.Clear() Exception
 Originally Posted by MikeVallotton
I've never seen anyone do it the way you are trying to there.
True. I don't the way the loop is constructed is wrong (odd as it is). I see what he was trying to express. The way the code is written it would have been better to express with a while loop
Code:
while(lvErrors.SelectedItems.Count > 0)
{
lvErrors.SelectedItems[0].Remove();
}
 Originally Posted by heba.farouk
I got that exception while using: ListView.Items.Clear()
((An Unhandled exception of type 'System.ArgumentOutOfRangeException' occured in system.windows.forms.dll
Additional Information: Specified argument was out of the range of valid values.))
Given what the original post said something is not quite right which is why I asked for the complete exception message. That will actually show where the program is failing. I suspect its the method that MikeVallotton has highlighted. I think its to do with how the items are being removed the from lvErrors.Items collection.
-
March 13th, 2008, 02:51 PM
#7
Re: ListView.Items.Clear() Exception
Yeah, the compiler in my head told me that his loop should work fine, but then again, CSC frequently doesn't agree with the compiler in my head.
The other way will make it easier for other developers (or him later) to quickly read his code, anyway.
-
March 14th, 2008, 04:04 AM
#8
Re: ListView.Items.Clear() Exception
 Originally Posted by heba.farouk
Hello
I got that exception while using: ListView.Items.Clear()
((An Unhandled exception of type 'System.ArgumentOutOfRangeException' occured in system.windows.forms.dll
ListView.Items.Clear() will never throw a 'System.ArgumentOutOfRangeException' because it doesnt take any arguments!
Rewrite your post so it makes sense
-
March 26th, 2008, 06:24 AM
#9
Re: ListView.Items.Clear() Exception
i don't think that the problem is in my code, even though, why lvErrors.Items.Clear() throws the same exception
Thanks
-
March 26th, 2008, 06:26 AM
#10
Re: ListView.Items.Clear() Exception
I assume you still have the problem. If so please put all the exception text. I would be glad to look at it and see what is the cause of the problem.
-
March 31st, 2008, 03:02 AM
#11
Re: ListView.Items.Clear() Exception
I found the problem 
i changed to use mouseUp event instead of mouseDown which made the problem because it was trying to select the deleted item and though throws an exception
Thanks all for help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|