0

My primary form launches a secondary form, which contains an owner-drawn tab control. The primary form then calls a public function in the secondary form to insert a tab page at a given index. I am consistently getting this exception inside my tabcontrol's DrawItem event handler:

System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '2' is not valid for 'index'.
Parameter name: index'

in this code:

private void tabControl_DrawItem(object sender, DrawItemEventArgs e)
{
     TabPage thisTab = tabControl.TabPages[e.Index];
...

The relevant call stack is:

    System.Windows.Forms.dll!System.Windows.Forms.TabControl.GetTabPage(int index)  Unknown
    System.Windows.Forms.dll!System.Windows.Forms.TabControl.TabPageCollection.this[int].get(int index) Unknown
>   tabControl_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) Line 751   C#
    System.Windows.Forms.dll!System.Windows.Forms.TabControl.OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.TabControl.WmReflectDrawItem(ref System.Windows.Forms.Message m)  Unknown
    System.Windows.Forms.dll!System.Windows.Forms.TabControl.WndProc(ref System.Windows.Forms.Message m)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)  Unknown
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    System.Windows.Forms.dll!System.Windows.Forms.Control.SendMessage(int msg, System.IntPtr wparam, System.IntPtr lparam)  Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ReflectMessageInternal(System.IntPtr hWnd, ref System.Windows.Forms.Message m)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.WmOwnerDraw(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.WmDrawItem(ref System.Windows.Forms.Message m)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m) Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)  Unknown
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.DefWndProc(ref System.Windows.Forms.Message m)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.TabControl.WndProc(ref System.Windows.Forms.Message m)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)  Unknown
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.DefWndProc(ref System.Windows.Forms.Message m)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.TabControl.WndProc(ref System.Windows.Forms.Message m)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)  Unknown
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    System.Windows.Forms.dll!System.Windows.Forms.TabControl.InsertItem(int index, System.Windows.Forms.TabPage tabPage)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.TabControl.TabPageCollection.Insert(int index, System.Windows.Forms.TabPage tabPage)  Unknown

Inside the public routine in the second form which is invoked by the primary form, I'm inserting a new tab page at index=1 of the tab control, and the tab control by default starts with two tabs. At the time of this exception, the TabPages member of the tab control only has the two default tab pages, and I am in the call stack of the public function that has already created the third tab page (at index=1) via the TabPages.Insert(<index>, <TabPage>) routine.

This started happening when I changed the secondary form to be non-dialog (meaning from the parent I call Show() instead of ShowDialog()), so that both forms could be interacted with at the same time. I tried adding focus to the second form prior to changing the tab pages, but it did not help.

2
  • please provide a minimal example(during that you may find the root cause yourself).
    – Lei Yang
    Commented Mar 15, 2022 at 13:06
  • You need to add a Tab Page to the control for each tab page to the instance of the control. Error indicates that you do not have 3 (index 2) tab pages. The exception looks recursive that the exception is triggering and second exception of the same type.
    – jdweng
    Commented Mar 15, 2022 at 13:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.