Disable Tab Control Tabs

G

Guest

I am trying to disable the tab control tabs programatically....setting the disable property of the tabpage disables the tabpage but the tab itself is still active and the text is not "greyed"...I can't even set the text color to a grey....it seems that the font etc is not available to be manipulated
 
H

Herfried K. Wagner [MVP]

* =?Utf-8?B?VGlt?= said:
I am trying to disable the tab control tabs programatically....setting
the disable property of the tabpage disables the tabpage but the tab
itself is still active and the text is not "greyed"...I can't even set
the text color to a grey....it seems that the font etc is not available
to be manipulated

There is no easy way to do that.

You can prevent the user from changing the tab selection:

<http://www.google.de/[email protected]>

Then you can draw the headers of the tabs yourself:

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

You can use 'ControlPaint.DrawStringDisabled' to draw a string "disabled".
 
É

Éric Moreau

Here is a class you can add to your project that gives you a real Enabled
property:

Option Strict On

'===========================================================================
==========
' Classe : TabControlEx.vb
'
' Auteur : Eric Moreau
' Concept S2i inc.
'
' Description : System.Windows.Forms.TabControl Extender
'
' Utilisation : 1. Add a regular TabControl to your form
' 2. In the " Windows Form Designer generated code "
section,
' replace System.Windows.Forms.TabControl by
TabControlEx (2 places)
' 3. In the Form_Load event (or a similar place), add this
line
' YourTabControlName.DrawMode =
TabDrawMode.OwnerDrawFixed
' 4. If you want to disable a tabpage, add this line
' YourTabControlName.DisablePage(YourTabPageName)
'
' Historique :
' Auteur Date Intervention
' ----------------- --------------- ----------------------------------------
---------
' Eric Moreau 2004/02/15 Création
'===========================================================================
==========

Public Class TabControlEx
Inherits System.Windows.Forms.TabControl

Private Const WM_LBUTTONDOWN As Integer = &H201

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_LBUTTONDOWN Then
Dim pt As New Point(m.LParam.ToInt32)
Dim index As Integer
For index = 0 To Me.TabPages.Count - 1
If GetTabRect(index).Contains(pt) Then
If TabPages(index).Enabled Then
MyBase.WndProc(m)
End If
Exit Sub
End If
Next
End If
MyBase.WndProc(m)
End Sub

Protected Overrides Sub OnKeyDown(ByVal ke As
System.Windows.Forms.KeyEventArgs)
Dim currentIndex As Integer = Me.SelectedIndex
Dim index As Integer
If ke.KeyCode = Keys.Left AndAlso Not (ke.Alt AndAlso Not
ke.Control) Then
For index = currentIndex - 1 To 0 Step -1
If TabPages(index).Enabled Then
Me.SelectedIndex = index
Exit For
End If
Next
ke.Handled = True
ElseIf ke.KeyCode = Keys.Right AndAlso Not (ke.Alt AndAlso Not
ke.Control) Then
For index = currentIndex + 1 To TabPages.Count - 1
If TabPages(index).Enabled Then
Me.SelectedIndex = index
Exit For
End If
Next
ke.Handled = True
End If
MyBase.OnKeyDown(ke)
End Sub

Public Sub DisablePage(ByRef pTabPage As TabPage)
With pTabPage
.Enabled = False
'.Text = "(* " & .Text & " *)"
End With
End Sub

Private Sub TabControlEx_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles MyBase.DrawItem
Dim intOffsetLeft As Int32
Dim intOffsetTop As Int32
Dim r As RectangleF = RectangleF.op_Implicit(e.Bounds)
Dim r2 As RectangleF
Dim ItemBrush As New SolidBrush(Me.BackColor)
Dim b As Brush ' SolidBrush = New
SolidBrush(TabControl1.ForeColor)
If Me.TabPages(e.Index).Enabled Then
b = Brushes.Black
Else
b = Brushes.Gray
End If

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

Dim im As Bitmap
If Me.TabPages(e.Index).ImageIndex <> -1 Then
im = CType(Me.ImageList.Images(Me.TabPages(e.Index).ImageIndex),
Bitmap)
End If

If Me.TabPages(e.Index).ImageIndex <> -1 Then
r2 = New RectangleF(r.X + (im.Width \ 2), r.Y, r.Width,
r.Height)
Else
r2 = New RectangleF(r.X, r.Y, r.Width, r.Height)
End If

If CBool(e.State And DrawItemState.Selected) Then
e.Graphics.FillRectangle(ItemBrush, e.Bounds)
e.Graphics.DrawString(Me.TabPages(e.Index).Text, e.Font, b, r2,
sf)
'e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text,
e.Font, Brushes.Red, r2, sf)
intOffsetLeft = 5
intOffsetTop = 5 '4
Else
e.Graphics.DrawString(Me.TabPages(e.Index).Text, e.Font, b, r2,
sf)
'e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text,
e.Font, Brushes.Blue, r2, sf)
intOffsetLeft = 2
intOffsetTop = 2 '4
End If

If Me.TabPages(e.Index).ImageIndex <> -1 Then
Me.ImageList.Draw(e.Graphics, Convert.ToInt32(r.Left) +
intOffsetLeft, Convert.ToInt32(r.Top) + intOffsetTop,
Me.TabPages(e.Index).ImageIndex)
End If
End Sub
End Class


--

HTH

Éric Moreau, MCSD
Conseiller Principal / Senior Consultant
Concept S2i inc.(www.s2i.com)
 
H

Herfried K. Wagner [MVP]

* "Éric Moreau said:
Here is a class you can add to your project that gives you a real Enabled
property:

Thank you for sharing the code with the group.
Public Sub DisablePage(ByRef pTabPage As TabPage)

Why do you pass the 'TabPage' 'ByRef' here?
 
J

Jon Gilbert

Hi!

I wanted to do this too. It works for me with (KISS):

For i As Integer = tabcontrol1.TabPages.Count To myTabCounter Step -1
Me.TabControl1.TabPages.Item(i).Dispose()
Next

myTabCounter contains the number of Tabs you want to keep (count them while
filling them). I seem to have another problem though: how do I set the
active tab / field to being the first field on the first tab. I've tried all
sorts of things (.select, .focus, .bringtofront). Can anyone help? Thanx

--
Jon(dot)Gilbert(at)Net-Entwicklung.de


Tim said:
I am trying to disable the tab control tabs programatically....setting the
disable property of the tabpage disables the tabpage but the tab itself is
still active and the text is not "greyed"...I can't even set the text color
to a grey....it seems that the font etc is not available to be manipulated
 

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