2

I have a tab control and to put a close button in the tab title I used:

private void systemRecordTabControl_DrawItem_1(object sender, DrawItemEventArgs e)
    {           
        e.Graphics.DrawImage(Properties.Resources.icon, e.Bounds.Right - 15, e.Bounds.Top + 3, 11, 11);
        e.Graphics.DrawString(systemRecordTabControl.TabPages[e.Index].Text, new Font("Arial", 12, FontStyle.Underline), Brushes.Black, e.Bounds.Left + 12, e.Bounds.Top + 4);
        e.DrawFocusRectangle();
    }

When the program is running, each time I switch between the tabs, the title text gets drawn again on top of the current tab text which causes it to be quite an eye sore

examples:

Eewwwww

vs.

Mmmmmm

Is there anyway I can stop this from happening? I don't know how to check if its drawn already

Edit: As a side note this only happens when I set the tab page appearance to be "Buttons" while the DrawMode property is "OwnerDrawFixed", also changing the focus to another control causes the currently selected tab page to return to normal(no text overlapping)

Mark Buckley
  • 75
  • 1
  • 1
  • 7
  • You should clear it and then redraw, the redraw can happen for a lot of reasons (moving a window over the control for example), so you should clear whatever is there and redraw, otherwise you can look at the paint event arguments and (I'm working from memory) it should have an invalidated region that it is requesting redrawn which may not be the entire thing. – Ron Beyer Apr 05 '18 at 01:01
  • I'm not sure this is what you meant but I tried e.Graphics.Clear() and it requires a color to redraw over the area, I used the normal background color but it causes the tab pages to disappear when a new one is created – Mark Buckley Apr 05 '18 at 01:09
  • 2
    Use [DrawBackground](https://msdn.microsoft.com/en-us/library/system.windows.forms.drawitemeventargs.drawbackground(v=vs.110).aspx). – Alexander Petrov Apr 05 '18 at 02:14
  • Take a look at [TabControl with Close and Add Button](https://stackoverflow.com/a/36900582/3110834). You can download a working example. – Reza Aghaei Apr 05 '18 at 05:30

1 Answers1

1

Just redraw the background:

    private void systemRecordTabControl_DrawItem_1(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground(); //<-- Redraw background       
        e.Graphics.DrawImage(Properties.Resources.icon, e.Bounds.Right - 15, e.Bounds.Top + 3, 11, 11);
        e.Graphics.DrawString(systemRecordTabControl.TabPages[e.Index].Text, new Font("Arial", 12, FontStyle.Underline), Brushes.Black, e.Bounds.Left + 12, e.Bounds.Top + 4);
        e.DrawFocusRectangle();
    }
Giancarlo Melis
  • 697
  • 4
  • 17
  • Thank you, It works but now there is a blue background on the tab title, I've tried changing the back color on everything to do with the tab control but nothing is changing it. Its not the end of the world but it doesn't fit in with the color scheme of my form – Mark Buckley Apr 05 '18 at 15:20
  • just add: e.Graphics.FillRectangle(new SolidBrush(BackColor), e.Bounds); after e.DrawBackground(); – Giancarlo Melis Apr 05 '18 at 15:31