Solved

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

Posted on 2006-11-02
4
243 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

23 Experts available now in Live!

Get 1:1 Help Now