我正在隨機獲取下面的異常。工具欄菜單是動態創建的。System.ArgumentOutOfRangeException Toolstrip菜單
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;
}
}
}
}
給我們更多的細節。上下文請?當你遇到這個錯誤時,你會做什麼? –
所以這是非常難以解決的朋友。沒有上下文中的代碼,錯誤對我們來說並不意味着什麼。那有意義嗎? –
請向我們顯示您的代碼。否則,我們不能幫助。 – kviiri