Solved

Why am I getting an out of rang exception with the following "for loop"?

Posted on 2006-11-02
4
244 Views
Last Modified: 2010-04-16
           int dropDownCount = actionMenu.DropDownItems.Count;
            if(dropDownCount > 6)
            {
                for (int index = 6; index < dropDownCount; index++)
                {
                    actionMenu.DropDownItems.RemoveAt(index);

                }
            }

Thanks.
0
Comment
Question by:lapucca
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 17864194
If you are trying to remove all but the first six items, try the following code:

  int dropDownCount = actionMenu.DropDownItems.Count;
  for (int index = 6; index < dropDownCount; index++)
    actionMenu.DropDownItems.RemoveAt(6);

Or,

  int dropDownCount = actionMenu.DropDownItems.Count;
  for (int index = (dropDownCount - 1); index >= 6; index--)
    actionMenu.DropDownItems.RemoveAt(index);
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17864198
Or even,

  while(actionMenu.DropDownItems.Count > 6)
    actionMenu.DropDownItems.RemoveAt(6);
0
 

Author Comment

by:lapucca
ID: 17864199
Nop, I was trying to remove all items after the 6th item.  I got it working now.  You see, the count is changing with each item I remove so it eventually ran out and got the exception error.  Thanks.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 17864204
I believe the reason is that you alter underlying collection, and therefore alter the count of items.

Try to use 'while' loop:

while(actionMenu.DropDownItems.Count>6){
    actionMenu.DropDownItems.RemoveAt(6);
}
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now