【发布时间】: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