10

I have a tab control and need to remove the dotted focus rectangle around the selected tab.

I have set the TabStop property of the TabControl to false. However if I click on a tab and press the Tab key, the dotted rectangle appears around the tabname.

I have tried creating my own TabControl and tried this

class MyTabControl : TabControl
{
        public MyTabControl()
        {
            TabStop = false;
            DrawMode = TabDrawMode.OwnerDrawFixed;
            DrawItem += new DrawItemEventHandler(DoMoreTabControl_DrawItem);
            Invalidate();
        }
}

However, the dotted rectangle still appears.

I also tried overriding the MyTabControl.OnPaint() method but it doesn't help.

Is there any way to achieve this?

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Karthik
  • 143
  • 2
  • 10

2 Answers2

4

Set the focus to tab instead of header (like this)

private void tabControl1_Click(object sender, EventArgs e)
{
    (sender as TabControl).SelectedTab.Focus();
}

You will see dotted rectangle for a millisecond, as soon as the above event gets executed it will disappear.

Also, to remove dotted rectangle for default selected tab on load

private void tabControl1_Enter(object sender, EventArgs e)
{
    (sender as TabControl).SelectedTab.Focus();
}

Both this changes worked for me! hope it helps somebody.

JD-V
  • 3,336
  • 1
  • 17
  • 20
2

Yes, DrawItem event. You didn't post it, impossible to guess what's wrong with it. Just make sure that you don't call e.DrawFocusRectangle(), likely to present when you copied the MSDN sample code. Simply deleting the statement is sufficient. Consider using a different background color or text font style as an alternative so the focus hint isn't entirely lost.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 4
    A good example of an application that does this: Visual Studio and Microsoft SQL Server Management Studio. Also web browsers. There is no focus rectangle on the tabs. Ctrl+tab appears to the accepted way to navigate tabs from the keyboard. – Chris Weber Feb 20 '12 at 22:48