CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2008
    Posts
    9

    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

  2. #2
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    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....

  3. #3
    Join Date
    Mar 2008
    Posts
    9

    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

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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.

  5. #5
    Join Date
    Mar 2008
    Posts
    72

    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.

  6. #6
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: ListView.Items.Clear() Exception

    Quote 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();
       }
    Quote 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.

  7. #7
    Join Date
    Mar 2008
    Posts
    72

    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.

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: ListView.Items.Clear() Exception

    Quote 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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Mar 2008
    Posts
    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

  10. #10
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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.

  11. #11
    Join Date
    Mar 2008
    Posts
    9

    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
  •  





Click Here to Expand Forum to Full Width

Featured