views:

497

answers:

8

My users are having an intermittent error when using a Windows Forms application built in VB.NET 3.5. Apparently when they click on the form and the form re-paints, a red 'X' will be painted over the MenuStrip control and the app will crash with the following error. Has anyone seen this before? Can someone point me in the right direction?

Thanks in advance,

Joe

    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)
A: 

Are you adding items to this strip dynamically?

Ben Scheirman
A: 

Need more info.

This is a dynamic menu isn't it.

FlySwat
A: 

Actually yes, there is one menu that has items added when the form is shown. The error occurs after the form has been shown. Could this still be a problem?

joek1975
A: 

You will have to find where in the the code this is happening but it is being cause by an integer variable being used to access your dynamic menu. Before you use the menu, use an if statement to make sure it is between 0 and the size of the collection - 1. Also, place a break point where you create the variable and step through the code watching what happens to it.

Also, a code sample of how you are using the dynamic menu would help.

Maudite
+1  A: 

@maudite, judging from the stacktrace, he is accessing the item using an indexer, and may not actually have a local variable named index.

FlySwat
A: 

While looking through the code, I discovered that the menu is being cleared and reloaded whenever the form data is being refreshed. The menu only needs to be loaded once, when the form is initially loaded.

I think that the menu may be getting cleared while the form is in the process of being painted. Do you think that this may be true?

joek1975
A: 

Thanks to all of you that helped to point me in the right direction. I made a change to only clear/add the menu when the form is loaded, so I shouldn't see this error again when the form is painting.

joek1975
A: 

I've the same trace in .net 2005 in multithread application (may be not mather) irregular crash with this exception. No predictable.

en System.Collections.ArrayList.get_Item(Int32 index) en System.Windows.Forms.ToolStripItemCollection.get_Item(Int32 index) en System.Windows.Forms.ToolStrip.OnPaint(PaintEventArgs e) en System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) en System.Windows.Forms.Control.WmPaint(Message& m) en System.Windows.Forms.Control.WndProc(Message& m) en System.Windows.Forms.ScrollableControl.WndProc(Message& m) en System.Windows.Forms.ToolStrip.WndProc(Message& m) en System.Windows.Forms.StatusStrip.WndProc(Message& m) en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) en System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) en System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) en System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) en System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) en System.Windows.Forms.Application.Run(ApplicationContext context)