开发者

Stretch tab headers instead of tab pages in tab control when resizing

I am owner-drawing a left-oriented tabcontrol in winforms.

Each tabpage has a fixed size, so when the UI is stretched wide, I would like the ItemSize width of the tab headers to increase correspondingly.

  开发者_如何学运维  private void tbcTests_Resize( object sender, EventArgs e )
    {
        tbcTests.ItemSize = new Size(
            tbcTests.Width - tbcTests.TabPages[0].Controls[0].Width - tbcTests.Padding.X,
            tbcTests.ItemSize.Height );
    }

This code results in a stack overflow. I suspect this is because the resize is done using incorrect dimensions, forcing the control to continuously redraw. However, I am unsure how to fix it. Am I not accounting for excess space correctly?

How should I resize the tab headers and using what dimensions?


Changing the ItemSize property cause the Resize event to fire again. You'll need a helper variable to suppress the nested event. Like this:

private bool busySizing;

private void tbcTests_Resize( object sender, EventArgs e )
{
    if (busySizing) return;
    busySizing = true;
    try {
       tbcTests.ItemSize = new Size(
          tbcTests.Width - tbcTests.TabPages[0].Controls[0].Width - tbcTests.Padding.X,
          tbcTests.ItemSize.Height );
    }
    finally {
       busySizing = false;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜