在第二个表单上插入选项卡页时出现异常

发布于 2025-01-14 08:31:09 字数 5474 浏览 2 评论 0原文

我的主窗体启动一个辅助窗体,其中包含一个所有者绘制的选项卡控件。然后,主窗体调用辅助窗体中的公共函数,以在给定索引处插入选项卡页。我一直在我的 tabcontrol 的 DrawItem 事件处理程序中遇到此异常:

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

在此代码中:

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

相关的调用堆栈是:

    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

在由主表单调用的第二个表单中的公共例程中,我插入在选项卡控件的index=1处新建一个选项卡页,选项卡控件默认以两个选项卡开始。发生此异常时,选项卡控件的 TabPages 成员只有两个默认选项卡页,而我在已经创建了第三个选项卡页的公共函数的调用堆栈中(位于index=1) 通过 TabPages.Insert(,) 例程。

当我将辅助窗体更改为非对话框时(这意味着我从父窗体调用 Show() 而不是 ShowDialog()),这种情况开始发生,这样两种窗体都可以同时进行互动。我尝试在更改选项卡页面之前将焦点添加到第二个表单,但这没有帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文