Tabpages

A

Alexia

Hi,

I wanted to know how i can make the title that is
displayed in a tabpage bold when it is selected.
I have 4 tabpages and i want the user to know more
clearly which one is selected.

I tried to do this but for some reason all the text on
that particular page turns bold.
Can any one give me some pointers?

Thx in advance.


btw im using VB.NET
 
M

Mick Doherty

You'll have to set the TabControls DrawMode property to OwnerDrawFixed and
draw the text in the TabControls DrawItem procedure.

VB.Net example:
\\\
Private Sub TabControl1_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles TabControl1.DrawItem

Dim r As RectangleF = RectangleF.op_Implicit(e.Bounds)
Dim ItemBrush As New SolidBrush(TabControl1.BackColor)
Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center

If CBool(e.State And DrawItemState.Selected) Then
e.Graphics.FillRectangle(ItemBrush, e.Bounds)
e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, _
New Font(e.Font, FontStyle.Bold), _
Brushes.Black, _
r, sf)
Else
e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, _
e.Font, _
Brushes.Black, _
r, sf)
End If

End Sub
///
Note that if the Tabpage Font does not support Fonstyle.Bold then the
function will fail. In this case use:
\\\
New Font(Font.FontFamily.GenericSansSerif, _
e.Font.SizeInPoints, _
FontStyle.Bold, _
GraphicsUnit.Point)
///
in place of:
New Font(e.Font, Fonstyle.Bold)

If you have XP Visual Styles then a lot more work is required as you'll have
to PInvoke various uxtheme.dll Functions in order to draw the TabPage Header
Items.
 
A

Alexia

Hi Mick

where do i write this code? Does it go in the onclick
event for each tabpage?
does the actual tabpage title not have a bold function
that will work specifically for just that text and not
everything else on that page?

thx
-----Original Message-----
You'll have to set the TabControls DrawMode property to OwnerDrawFixed and
draw the text in the TabControls DrawItem procedure.

VB.Net example:
\\\
Private Sub TabControl1_DrawItem(ByVal sender As Object, _
ByVal e As
System.Windows.Forms.DrawItemEventArgs) _
 
M

Mick Doherty

Just copy and paste the code as its own procedure.
This code is written in the DrawItem event. The first line of the code gives
you a clue:
Private Sub TabControl1_DrawItem(....) Handles TabControl1.DrawItem

if it was meant to be in the click event the first line would have read:
Private Sub TabControl1_Click(....) Handles TabControl1.Click

Maybe you were confused by the Line Continuation characters I used in order
that Outlook would not autowrap the text to 76 characters and spoil the
code.

Of course this code is dependant upon your TabControl being called
TabControl1.
There is no property available to change the appearance of the Text of an
individual Tab.
The code draws all the TabPage HeaderItems of TabControl1, making only the
selected TabPages Text Bold.
 
A

Alexia

Hi Mick

i pasted the code that you provided me into my form. I
have a control named TabControl1 with 4 tabpages.
The problem i am having is that the application runs with
no build error but the text that appears in the tab does
not turn bold when i click on it.
Is there anything else i need to edit in your code before
i can use it?

thx in advance
 
A

Alexia

Hi Mick

thx for the code. it worked a treat. I just forgot to set
the Drawmode property to OwnerDrawFixed and that is whyi
couldnt get it to work.

Do u know of a way to change the background colour of the
tabpage when the relavant tab is selected?
 
M

Mick Doherty

Change the color of the ItemBrush. At the moment it is declared as:
Dim ItemBrush As New SolidBrush(TabControl1.BackColor)
Change that to:
Dim ItemBrush As New SolidBrush(Color.WhateverYouLike)

If you also want a custom color for the Non Selected items then add the
following two lines between Else and e.Graphics.DrawString(... :
ItemBrush.Color = Color.SomeOtherColor
e.Graphics.FillRectangle(ItemBrush, e.Bounds)

Obviously Color.WhateverYouLike and Color.SomeOtherColor wont work, but
hopefully you understand that.
 
P

PK

Hi,
I want similar functionality, but in some other button. My application
has
4 tab pages. In the final page we have review button where we do
validations
for all input. If some errors are found in particular tab page, we
want that
tab text to turn red. Errors we are finding from database or
whereever. we know
that which page has errors. But how to turn that particular tab to red
when button is clicked.

I would appreciate your response.

Thanks
PK
 
M

Mick Doherty

Hope you can make sense of this:

\\\
Private TabPageInvalid() As Boolean.

Private Sub Form_Load(...)...
Redim TabPageInvalid(TabControl1.TabPages.Count-1)
End Sub

Private Sub ValidationButton_Click(...)...
For each tp As TabPage in TabControl1.TabPages
If tp is not validated by your checks then
TabPageInvalid(tp.TabIndex) = True
End If
Next
TabControl1.Invalidate()
End Sub

Private Sub TabControl1_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles TabControl1.DrawItem

Dim r As RectangleF = RectangleF.op_Implicit(e.Bounds)
Dim ItemBrush As New SolidBrush(TabControl1.BackColor)
Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center

e.Graphics.FillRectangle(ItemBrush, e.Bounds)

If TabPageInvalid(e.Index) Then
e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, _
e.Font, _
Brushes.Red, _
r, sf)
Else
e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, _
e.Font, _
SystemBrushes.ControlText, _
r, sf)
End If

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