Solved

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

Posted on 2006-11-02
4
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 30

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
placing a checked in the checklistbox based on the value in database table. 3 44
WPF user control poped up from a window 1 35
WPF Select Items 3 26
c#, case, if 4 19
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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