Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I remove the dotted focus rectangle over tabs on a TabControl?

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?

like image 863
Karthik Avatar asked Mar 07 '11 06:03

Karthik


2 Answers

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.

like image 125
JD-V Avatar answered Nov 05 '22 03:11

JD-V


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.

like image 27
Hans Passant Avatar answered Nov 05 '22 05:11

Hans Passant