Make an improved owner-drawn TabControl in C#

[owner-drawn TabControl]

The post Make an owner-drawn TabControl in C# shows how to make an owner-drawn tab control. Unfortunately the control behaves strangely if it’s not wide enough to display all of its tabs at once. If the control is too small and you click the last tab, the control scrolls its tabs to the left before selecting the tab you clicked.

That’s strange but not harmful, but something worse happens if you click the X button on the last tab to delete it. In that case, the original example’s code calls the control’s RemoveAt method to remove the last tab. Unfortunately for some reason I haven’t been able to figure out, the control deletes the second-to-last control instead.

Fortunately this is easy to work around. Instead of using this code to delete tab number i:

tabMenu.TabPages.RemoveAt(i);

The new example uses the following statement to delete that same tab.

tabMenu.TabPages.Remove(tabMenu.TabPages[i]);

This change shouldn’t make a difference, so the fact that it fixes the problem shows there’s something wrong inside the TabControl.

The control still scrolls its tabs unexpectedly, but at least it’s no longer removing the wrong tab.

Of course an alternative strategy would be to size the control so it can always display all of its tabs, but that may not always be an option.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.

This entry was posted in controls and tagged , , , , , , , , , . Bookmark the permalink.

One Response to Make an improved owner-drawn TabControl in C#

  1. Pingback: Add tabs to a TabControl at runtime in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *