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

可紊 提交于 2019-12-04 19:32:55

问题


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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/5216764/can-i-remove-the-dotted-focus-rectangle-over-tabs-on-a-tabcontrol

工具导航Map

JSON相关