Customize Tab Control

S

scorpion53061

I am trying to build a new tabcontrol while inheriting the tabcontrol in
order to change the ,menu color of the tab control as it appears it won't
let me do it with the normal control.

With a customized control when I specify background color it won't allow it
either apparently.

Anyone know why and how I can do this?
 
H

Herfried K. Wagner [MVP]

* "scorpion53061 said:
I am trying to build a new tabcontrol while inheriting the tabcontrol in
order to change the ,menu color of the tab control as it appears it won't
let me do it with the normal control.

With a customized control when I specify background color it won't allow it
either apparently.

Why not base your drawing code on this code written by Ken Tucker [MVP]?

<http://www.onteorasoftware.com/VBControls.aspx#AnsTQ1>
 
S

scorpion53061

this is great code but it does not address the problem of the
defaultbackcolor (read only) property I need to change.

Does anyone have a suggestion on how to change this? I tried "Shadows" and
it didnt work for some reason.
 
H

Herfried K. Wagner [MVP]

* "scorpion53061 said:
this is great code but it does not address the problem of the
defaultbackcolor (read only) property I need to change.

You cannot override a shared property.
 
A

Armin Zingler

scorpion53061 said:
this is great code but it does not address the problem of the
defaultbackcolor (read only) property I need to change.

Does anyone have a suggestion on how to change this? I tried
"Shadows" and it didnt work for some reason.

In addition to Herfried's reply: You cannot override it because a shared
property is tied to the type used to reference or to resolve the call. You
might Shadow the shared property in a derived class, but it would only be
called if you own code uses the type of your derived class. All the code
that refers to Control.DefaultBackColor still calls Control.DefaultBackColor
because that is the type used in the base classes - they don't know your
class.
 
S

scorpion53061

Thanks Armin.....

Have you seen any examples showing a derived class example that would be
similar in nature?
 
A

Armin Zingler

scorpion53061 said:
Thanks Armin.....

Have you seen any examples showing a derived class example that would
be similar in nature?

No. (short answer but I can't add anything :)
 
S

scorpion53061

no problem.

I hate being told I "can't" do something.

But if there is no way I guess I have to accept it.
 
M

Mick Doherty

There's always a way, just sometimes it is difficult to find, or a lot of
work.
In this case you need to fully take over the TabControls Drawing. Heres some
code for you to play with.

SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.ResizeRedraw Or _
ControlStyles.UserPaint Or _
ControlStyles.DoubleBuffer, True)

Private m_BackColor As Color = Color.Blue
<DefaultValue(GetType(Color), "Blue"), _
Browsable(True)> _
Public Shadows Property BackColor() As Color
Get
Return m_BackColor
End Get
Set(ByVal Value As Color)
If Not m_BackColor.Equals(Value) Then
m_BackColor = Value
Invalidate()
End If
End Set
End Property

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
e.Graphics.Clear(m_BackColor)
'Draw the tab items
Dim ItemBounds As Rectangle
Dim TextBrush As New SolidBrush(Color.Empty)
For TabIndex As Integer = 0 To Me.TabCount - 1
ItemBounds = Me.GetTabRect(TabIndex)

ControlPaint.DrawBorder3D( _
e.Graphics, ItemBounds, _
Border3DStyle.Bump, _
Border3DSide.All)

Dim sf As New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center

If Me.SelectedIndex = TabIndex Then
TextBrush.Color = Color.Red
Else
TextBrush.Color = Me.ForeColor
End If

e.Graphics.DrawString( _
Me.TabPages(TabIndex).Text, _
Me.Font, TextBrush, _
RectangleF.op_Implicit(Me.GetTabRect(TabIndex)), _
sf)

Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top