1

I have a TabControl where I want to keep the tabs to a fixed size and I want icons in the tabs. I have set TabControl.SizeMode = Fixed and TabControl.ItemSize = 100, 18. I have also set TabControl.ImageList and am assigning images to the tabs via TabPage.ImageKey.

Here is what it looks like if I comment-out assigning the ImageKey:

image1

And here is what it looks like if I am assigning the ImageKey:

image2

Is there some sort of "alignment" for the icons? I want them to be on the far left in the blank space, but instead they are starting where the text starts. Any suggestions?

(BTW - if I set TabControl.SizeMode = Normal, I get the tab content the way I want it, but the tabs aren't a fixed size):

image3

derekantrican
  • 1,891
  • 3
  • 27
  • 57
  • As I know, there is no direct way to do that, unless you draw the items by yourself through DrawItem events in tabControl. – Kaj Jun 08 '18 at 23:53
  • The visual styles renderer for TabControl has been notoriously buggy for a very long time. Yup, looks like they screwed it up again in Win10 Fall Creators edition. A workaround of sorts is to disable visual styles for only the tab control, not for the tab pages or the rest of your UI. https://stackoverflow.com/a/299983/17034 – Hans Passant Jun 09 '18 at 00:12

1 Answers1

1

I can verify the issue that you are seeing with TabControl.SizeMode = Fixed (on Windows 10). I initially seen it in the designer when configuring a TabPage with an icon. However the irritating thing is that the issue corrected itself if the designer is closed and reopened. This suggests a window style setting of some sort and there are some Tab Control Styles set in the CreateParams Property based on the SizeMode Property. However, I found no solution in attempting to apply the TCS_FORCEICONLEFT style. If the ImageIndex property is set prior to the control being shown, then the alignment is as desired. So I figured that there must be something being configured on handle creation.

If you call the form's RecreateHandle method after setting the TabPage.ImageIndex property, the form redraws and all looks good. However this cause the form to blink. Calling the Control.RecreateHandle method on the TabControl also works. This is a protected method and would necessitate using a derived TabControl to expose the method or you could use Reflection to invoke the method.

public class MyTC : TabControl
{
    public void FixIcon()
    {
        RecreateHandle();
    }
}
TnTinMn
  • 11,522
  • 3
  • 18
  • 39
  • You can have the same effect (after you have assigned the `ImageList` and the Items `ImageIndex`), setting: `tabControl1.SizeMode = TabSizeMode.Normal;` and immediately after: `tabControl1.SizeMode = TabSizeMode.Fixed;` (in the environments I could test - Win10 Fall Creators edition not included). – Jimi Jun 09 '18 at 00:36
  • Similarly to @Jimi 's idea - it actually worked for me to just set `tabControl.ItemSize = new Size(100, 18);` (the same size as set in the designer) *again* after setting the `tabPage.ImageKey` – derekantrican Jun 09 '18 at 19:47