1

I have a Windows Forms application with a TabControl that has two TabPage objects:

Tab pages in their default state

What I am aming for is a way to display an icon beside the titles of the tabs. For that, an ImageList is assigned to the TabControl so the ImageIndex property of the tab pages can be set. When I do that in the designer, it looks fine, but when changing it programatically in my code like this:

myTabControl.TabPages[0].ImageIndex = 0

, it has this weird overlapping effect:

Tab pages when edited programatically

How can I avoid this? Things I've tried are myTabControl.PerformLayout() and myTabControl.Invalidate(), but none of them worked. Thanks!

Totemi1324
  • 468
  • 1
  • 6
  • 19
  • 1
    Use [this](https://stackoverflow.com/a/7273650/14171304) workaround when you have the `SizeMode` property set to `TabSizeMode.Fixed`. The `ItemSize` property setter calls internal methods to recalculate the bounds and the parts (image and text) will be rendered correctly afterwards. – dr.null Dec 15 '22 at 02:12

1 Answers1

1

So apparently, there is a workaround solution:

myTabControl.ItemSize = myTabControl.ItemSize;

This only works if the SizeMode of the tab control is set to TabSizeMode.fixed. Thanks to Dr.Null (comment above) to point this out.

While this is simple though, it's not so elegant and confuses readers. I wonder whether there are other, intended solutions or whether this is a "hole" in WinForms Microsoft didn't consider.

Totemi1324
  • 468
  • 1
  • 6
  • 19