Gray-out the text on the tab control display?

  • Thread starter Thread starter KMiller
  • Start date Start date
I assume you don't know before loading the form which to enable/disable? If
so a contructor passed a tabindex would work fine.

I also assume that you have tried setting the enabled property for the
particular tab page that you want disabled...

Is this true?

Shane
 
At the time the form is assembled, I DO NOT know.
However, shortly thereafter (during the loading process),
I DO know whether they have access or not to the
particular tab. It's at this time when I'm attempting to
disable (gray-out) the tab text, but nothing works.

I've tried setting the enabled property to false for the
tab, but doing that does nothing to the tab text display,
and users have no way of knowing they DO NOT have access
to the tab other then by clicking into it and finding out
that nothing works because all the controls have been
disabled. Leaving the functionality this way is
inadequate and out of spec.
 
You'll have to OwnerDraw the Tabs if that's the appearance you want.
Why not simply remove the Tabpages that the user has no access to?
 
Hi K,

wow, I see your point.

I thought I had it fixed in my app too but my titles aren't disabled either.

You can do ownerdrawn fixed and change the color of the font easy enough.

Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
Dim tp As TabPage
Dim layoutrct As New RectangleF(e.Bounds.Left, e.Bounds.Top,
e.Bounds.Width, e.Bounds.Height)
Dim sf As New StringFormat(StringFormatFlags.FitBlackBox)
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center

tp = TabControl1.TabPages(e.Index)
If Not tp.Enabled Then
'for some reason not printing the 3-d part--tried to print twice
with offset but no luck
e.Graphics.DrawString(tp.Text, e.Font, SystemBrushes.ControlLight,
layoutrct)
layoutrct.Offset(2, 2)
e.Graphics.DrawString(tp.Text, e.Font, SystemBrushes.ControlDark,
layoutrct) 'SolidBrush(e.BackColor), layoutrct)
Else
e.Graphics.DrawString(tp.Text, e.Font,New SolidBrush(e.BackColor),
layoutrct)
End If
End Sub

This works if items are not enabled. But there must be an easier way--also
they shouldn't even be able to click on the tab.

I haven't found out how to avoid that.

this is a really big pain for something simple--but it seems that most of
the control included are lacking in very bacic features and require lots of
extra coding to just do standard things.

I hope you find an answer--I would also like to know.

Shane
 
Mick,

I told her the same thing, but how do you draw them with the normal 3d
enabled look?

I tried drawstring using a layout rect and then offset(1,1) or offset(2,2)
on it and drew again using the same with a different color but it doesn't
work.

Also,
disabled means you shouldn't even be able to select the tab in the first
place. How can this be accomplished?

Thanks,

Shane

"Mick Doherty"
 
Thanks for the responses to all..

Seems like a lot of trouble to execute a very simple
change. We purchases the Infragistics UltraDev suite a
while back, which includes a tab control, but we had not
been using it. I just checked it out and sure enough, it
grays-out the text by simply disabling the tab. I may
just swap out by MS tab for this one.

K
 
Your reply was close. Use ControlPaint.DrawStringDisabled() not
Graphics.DrawString() twice.
As for not being able to select Disabled Tabpages, that can be emulated,
although I feel it is better to remove the TabPage than display it as
Disabled.

Code follows:

\\\
Private ValidPageIndex As Integer = 0

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

Dim CurrentTabPage As TabPage = TabControl1.TabPages(e.Index)
Dim r As RectangleF = RectangleF.op_Implicit(e.Bounds)
Dim ItemBrush As New SolidBrush(TabControl1.BackColor)
Dim TextBrush As New SolidBrush(TabControl1.ForeColor)
Dim sf As New StringFormat

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

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

If CurrentTabPage.Enabled = False Then
ControlPaint.DrawStringDisabled(e.Graphics, _
CurrentTabPage.Text, _
e.Font, _
ItemBrush.Color, _
r, sf)
Else
e.Graphics.DrawString(CurrentTabPage.Text, _
e.Font, _
TextBrush, _
r, sf)
End If

End Sub

Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged

If TabControl1.SelectedTab.Enabled = False Then
TabControl1.SelectedIndex = ValidPageIndex
Else
ValidPageIndex = TabControl1.SelectedIndex
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
TabPage2.Enabled = False
End Sub
///
 
cool, never have noticed that method.

This should have been implemented in the control to begin with.

Thanks,

Shane

"Mick Doherty"
 
That or you could try to make your own control inheriting from the tab
control, "K's Tab Control"

Just add that functionality and display some interface for disabling
tabs....

You're right it's a pain and like most things I've found in .net, is a
standard expectation that should have been implemented in the original
control. (guess they want to make sure you go with 3rd part vendors and not
put them out of business or something.) Still there is lots of room for 3rd
party controls and I agree with you that things like this are just bare
minimum expected behaviors.

Removing the tab does seem to be a lot easier. I suggest that route unless
you want to make your own reusuable control--which shouldn't be too hard....
;) maybe.

Shane
 
Removing the tab does seem to be a lot easier. I suggest that route
unless
you want to make your own reusuable control--which shouldn't be too hard....
;) maybe.
Much harder than you'd think. I'm currently working on a new TabControl and
you wouldn't believe the amount of problems I'm coming up against, but it is
coming along.
 
Wow!

I guess the point is that we shouldn't have to do it in the first place.
That should have been included in any standard tab control I would think.


"Mick Doherty"
 
Back
Top