我的用户在使用 VB.NET 3.5 中内置的 Windows 窗体应用程序时遇到间歇性错误。显然,当他们单击表单并重新绘制表单时,将在 MenuStrip 控件上绘制一个红色“X”,并且应用程序将崩溃并出现以下错误。

有没有人见过这个?有人能指出我正确的方向吗?

    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.ToolStripItemCollection.get_Item(Int32 index)
   at System.Windows.Forms.ToolStrip.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.MenuStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
有帮助吗?

解决方案

您是否动态地将项目添加到该条中?

其他提示

您必须找到代码中发生这种情况的位置,但这是由用于访问动态菜单的整数变量引起的。在使用菜单之前,请使用 if 语句确保它介于 0 和集合大小 - 1 之间。另外,在创建变量的位置放置一个断点,并单步执行代码,观察发生了什么。

此外,有关如何使用动态菜单的代码示例也会有所帮助。

在查看代码时,我发现每当刷新表单数据时,菜单就会被清除并重新加载。菜单只需在最初加载表单时加载一次。

我认为在绘制表单的过程中菜单可能会被清除。您认为这可能是真的吗?

感谢你们所有人为我指明了正确的方向。我进行了更改,仅在加载表单时清除/添加菜单,因此在绘制表单时我不应该再次看到此错误。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top