Jump to content

tabcontrol drawitem problem

- - - - -

  • Please log in to reply
3 replies to this topic

#1
sp3tsnaz

sp3tsnaz

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Font TabFont;
            
            Brush BackBrush = new SolidBrush(Color.FromArgb(102,102,102)); //Set background color
            Brush ForeBrush = new SolidBrush(Color.FromArgb(180,255,0));
            //Brush ForeBrush = new SolidBrush(Color.Yellow);//Set foreground color
            if (e.Index == this.tabControl1.SelectedIndex)
            {
                TabFont = new Font(e.Font,FontStyle.Regular|FontStyle.Bold );
            }
            else
            {
                TabFont = e.Font;
            }            
            string TabName = this.tabControl1.TabPages[e.Index].Text;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;            
            e.Graphics.FillRectangle(BackBrush, e.Bounds);                     
            Rectangle r = e.Bounds;            
            r = new Rectangle(r.X, r.Y , r.Width, r.Height);            
            e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);
            
            //Dispose objects
            sf.Dispose();
            if (e.Index == this.tabControl1.SelectedIndex)
            {
                TabFont.Dispose();
                BackBrush.Dispose();
            }
            else
            {
                BackBrush.Dispose();
                ForeBrush.Dispose();
            }
        }

this is the code that i got from some forum , i edited it according to my own needs , but there is one problem .. i have not been able to paint the tab buttons properly using this code .. i mean its fine when i click it but when the tab is not in focus the border colors become default and it looks very ugly .. any ideas ... im attaching the screenshot ... please check that out ..

Thankyou in advance ..

Attached Files



#2
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
DrawItem does not give you such opportunity as to draw outside the tab's client area - and what you've drawn with back color that is the client area. To gain more control, you have to override Paint event of the TabControl and then to draw everything, which is probably too much. TabControl offers quite limited editing options.

Another option, which may require less coding, is to create your own control (or find one on the Web) with panel inside, which plays the role of the client area of selected tab. I don't see much more options than this...

#3
Johny_D

Johny_D

    Newbie

  • Members
  • Pip
  • 3 posts
There is simple way, to change Control.ForeColor = Color.Black, but I have to do it every time I disable control.

#4
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
That doesn't work on my system (Windows 7).

When Enable=false, custom set ForeColor is ignored and text is always drawn in some sort of dark gray color. BackColor is used as set even when Enabled=false.

When ReadOnly=true, custom set ForeColor is ignored unless BackColor is explicitly set. BackColor is not ignored.

MSDN on ForeColor and BackColor only says this:

Quote

This property might be over ridden if the ReadOnly property of the TextBoxBase is set to true.
But it doesn't say how is it affected... Even if you make the code work in some desired way, nobody can save you from disaster if this behavior is changed in next release of .NET Framework.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users