'Exception inserting tab page on a second form

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source