Modificando el TabControl de .NET a TabControlEx

0
4449

Agregar funcionalidades a los controles que proporciona el .NET Framework 1.x es algo que tenemos que hacer cuando estos no cumplen las necesidades de nuestra aplicación.
Introducción: En este artículo tratare de dar una instroducción al tema. En el ejemplo agregaremos la opcion de «deshabilitar» los TapPages.

Importante: Este articulo fue escrito en el 2005, puede estar obsoleto.

En .NET tenemos muchas opciones de controles, pero muchos de estos no vienen del todo completos, por eso tenemos que reprogramarlos nosotros mismos para agregarle las funcionalidades que necesitemos.

Uno de estos controles que utilizamos a menudo pero que a veces se nos queda corto es el TabControl, ya que no tiene la opcion para habilitar TapPages. Por seguridad en algunas aplicaciones se necesita que los usuarios no puedan entrar a determinado Tab.

Para esto vamos a crear un control llamado «TabControlEX» con esta nueva funcionalidad que utilizaremos luego en la aplicación de ejemplo.

Option Strict On

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)
pTabPage.Enabled = False
End Sub

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

Try
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
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)

intOffsetLeft = 5
intOffsetTop = 5
Else
e.Graphics.DrawString(Me.TabPages(e.Index).Text, e.Font, b, r2, sf)
intOffsetLeft = 2
intOffsetTop = 2
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
Catch ex As Exception
''The control is probably being disposed!!!
End Try
End Sub
End Class

Ahora, para usarlo, debe seguir estos simples pasos:

1. Añada un TabControl normal a su form al cual le agregará TabPages como hace normalmente.

2. Debe asegurarse de que su TabControl tiene la propiedad DrawMode puesta en OwnerDrawFixed. El mejor lugar para agregarla es en el evento Load de Form justo antes (o después) de la asignación del orden (order) del TabPage.
tabControl2.DrawMode = TabDrawMode.OwnerDrawFixed

Ahora está listo para deshabilitar el acceso a un TabPage. Para hacerlo, tan solo añada lo siguiente a su código:

TabControl2.DisablePage(TabPage6)

DEJA UNA RESPUESTA

Por favor ingrese su comentario!
Por favor ingrese su nombre aquí