【问题标题】:System.ArgumentOutOfRangeException Toolstrip menuSystem.ArgumentOutOfRangeException 工具条菜单
【发布时间】:2013-12-12 19:07:00
【问题描述】:

我随机收到以下异常。工具条菜单是动态创建的。

System.ArgumentOutOfRangeException - Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.Layout.ArrangedElementCollection.get_Item(Int32 index)
at System.Windows.Forms.Layout.FlowLayout.xLayoutRow(ContainerProxy containerProxy, ElementProxy elementProxy, Int32 startIndex, Int32 endIndex, Rectangle rowBounds, Int32& breakIndex, Boolean measureOnly)
at System.Windows.Forms.Layout.FlowLayout.xLayout(IArrangedElement container, Rectangle displayRect, Boolean measureOnly)
at System.Windows.Forms.Layout.FlowLayout.GetPreferredSize(IArrangedElement container, Size proposedConstraints)
at System.Windows.Forms.ToolStripDropDownMenu.ToolStripDropDownLayoutEngine.GetPreferredSize(IArrangedElement container, Size proposedConstraints)
at System.Windows.Forms.ToolStrip.GetPreferredSizeCore(Size proposedSize)
at System.Windows.Forms.Control.GetPreferredSize(Size proposedSize)
at System.Windows.Forms.ToolStripDropDown.GetSuggestedSize()
at System.Windows.Forms.ToolStripDropDown.AdjustSize()
at System.Windows.Forms.ToolStripDropDownMenu.OnLayout(LayoutEventArgs e)
at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
at System.Windows.Forms.ToolStripItem.InvalidateItemLayout(String affectedProperty, Boolean invalidatePainting)
at System.Windows.Forms.ToolStripDropDownItem.OnRightToLeftChanged(EventArgs e)
at System.Windows.Forms.ToolStripItem.OnOwnerChanged(EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnOwnerChanged(EventArgs e)
at System.Windows.Forms.ToolStripItem.SetOwner(ToolStrip newOwner)
at System.Windows.Forms.ToolStripItemCollection.SetOwner(ToolStripItem item)
at System.Windows.Forms.ToolStripItemCollection.Add(ToolStripItem value)

异常发生在以下方法中。将项目添加到 mnuRoot 时会发生这种情况。右键单击所选项目时调用此方法。

private static void BuildMenu(ToolStripMenuItem root, XMLSerItem mnuItem, ToolStrip mnuRoot, Dictionary<string, Image> dctIcons, CustomMenuClickHandler dlgEventHandler, ToolStripMenuItem mnuAddAfter, bool bHideDisabled)
        {
            if(root == null)
            {
                // Try to find an existing menu item
                ToolStripItem mnuMerge = FindMenuItem( mnuRoot.Items, mnuItem );
                if(mnuMerge == null)
                {
                    lock( mnuRoot.Items )
                    {
                            if (mnuAddAfter == null)
                            {             
                                mnuRoot.Items.Add(item);
                            }
                            else
                            {
                                mnuRoot.Items.Insert(mnuRoot.Items.IndexOf(mnuAddAfter), item);
                            }

                    }
                }
                else
                {
                    // Use a reference to the found item
                    item = mnuMerge;
                }
            }
            else
            {
                // Try to find an existing menu item
                ToolStripItem mnuMerge = FindMenuItem( root.DropDownItems, mnuItem );
                if(mnuMerge == null)
                {
                    lock( root.DropDownItems )
                    {
                        // Add the menu item to the root item
                        root.DropDownItems.Add( item );
                    }
                }
                else
                {

                    item = mnuMerge;
                }
            }         
        }
}

【问题讨论】:

  • 给我们更多细节。请问上下文????当您遇到此错误时,您会怎么做?
  • 所以这几乎是无法解决的朋友。如果没有上下文中的代码,该错误对我们没有任何意义。这有意义吗?
  • 请向我们展示您的代码。否则我们无能为力。
  • 这是一个使用工具条的上下文菜单。右键单击所选项目时会动态创建菜单。基于项目选择将菜单项添加到上下文菜单或从上下文菜单中删除。当我反复尝试同一组项目时,异常随机发生。

标签: c# winforms toolstripmenu


【解决方案1】:

尽管这个问题被否决是有充分理由的,但这是我在谷歌上搜索“xLayoutRow ArgumentOutOfRangeException”时得到的唯一结果,所以我仍然会在这里分享我的经验。

我在这篇文章中遇到了堆栈跟踪顶部的异常:

System.ArgumentOutOfRangeException - Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.Layout.ArrangedElementCollection.get_Item(Int32 index)
at System.Windows.Forms.Layout.FlowLayout.xLayoutRow(ContainerProxy containerProxy, ElementProxy elementProxy, Int32 startIndex, Int32 endIndex, Rectangle rowBounds, Int32& breakIndex, Boolean measureOnly)
at System.Windows.Forms.Layout.FlowLayout.xLayout(IArrangedElement container, Rectangle displayRect, Boolean measureOnly)
at System.Windows.Forms.Layout.FlowLayout.GetPreferredSize(IArrangedElement container, Size proposedConstraints)

就我而言,原因总结如下:

  • MyControl 类型的 UserControl 包含 FlowLayoutPanel
  • MyControl 的实例a 被实例化并添加到容器控件bControls 集合中:b.Controls.Add(a)
  • 这个Add操作期间,通过事件触发其他事件,b.Controls中的所有控件都被释放,包括a
  • 调用 b.Controls.Add(a) 后出现异常,该调用仍未完成。

当然,在将控件添加到控件集合时处理控件不是我的意图,找到原因让我可以立即解决问题。似乎发生的事情是 FlowLayoutPanel 尝试进行布局,但它和它的控件正在被删除并在飞行中处置,这可以理解地使它失败。

也许这有助于 OP 或遇到此异常的其他人。

【讨论】:

  • 这里我有一个上下文菜单用户控件。我需要更改布局类型来解决此问题吗?
【解决方案2】:

参数超出范围的异常文本说明如下:

索引超出范围。必须是非负数且小于集合的大小。

你声称这个异常发生在这一行:

mnuRoot.Items.Insert(mnuRoot.Items.IndexOf(mnuAddAfter), item);

从堆栈跟踪中我们了解到异常是在 Arraylist 的 Item 访问器处引发的。这意味着我们在 Insert 方法中提供的第一个参数必须是负数或大于 Arraylist 的大小。让我们首先分析我们的索引值是否很大。

索引是从mnuRootInsert 拥有的项目集合中获得的,该集合在mnuRoot 的同一集合中。值太大是非常不可能的,因为代码引用的是同一个集合。唯一可能出错的情况是,如果多个线程正在更新该 Arraylist,并通过判断 lock 语句来防止这种情况发生。

保留负数作为选项。如果对mnuAddAfter 的引用不在mnuRoot.Items 的集合中,则会出现这种情况。由于您已经在检查 null 我认为这是可能发生的情况。

根据提供的代码n-p,我假设您想将item 添加到mnuRoot.Items 集合中无论如何

ArrayListIndexOf 实现在搜索空值时不会中断,如果在列表中找不到1 一个,则该方法返回 -1。使用此知识,以下实现将防止参数超出范围异常。

var index = mnuRoot.Items.IndexOf(mnuAddAfter); // if mnuAddAfter is null -1 is returned
if (index == -1)
{             
   mnuRoot.Items.Add(item);
}
else
{
    mnuRoot.Items.Insert(index, item);
}

1.对于挑剔者:ToolStripItemCollection 保证不会在其集合中插入空值

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多