Disabling A Tab Page on A TAb Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a tabcontrol with three tab pages on it and I would like to prevent a
user from clicking on another tab page until he has finished entering data on
the current page.

Is there a way to disable the other tab pages like you would disable a button?
 
mh1mark,

One way is to set the TabPage Enabled property to False for any tab pages
you don't want the user editing. Another way is to temporarily remove (by
using the tab control's .TabPages.RemoveAt method) any tab pages from the Tab
control's collection and then adding them back in when needed.

HTH,
Tom
 
Problem is (I think) that even though the tab page is disabled, it can
still be clicked on.
 
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, Visual Developer - Visual Basic MVP
(http://aspnet2.com/mvp.ashx?EricMoreau)
Conseiller Principal / Senior Consultant
Concept S2i inc.(www.s2i.com)
 
Excellent!
Needs a little work though.

Multiline/SideAlign allows up and down keys to select disabled tabpage.
Ctrl+Tab and Ctrl+Shift+Tab also allows selection of disabled tabpage.

I keep meaning to fix this on my tabcontrol source but I have never got
around to it.
 
Back
Top