Question

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?

    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)
Was it helpful?

Solution

Are you adding items to this strip dynamically?

OTHER TIPS

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.

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?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top