Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
TabControl Page Enabled property
Message
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00876963
Message ID:
00877507
Views:
14
>I am building an application using Windows Forms (VB.Net 2003).
>
>I need to disable some pages of a tab control depending on users privileges. It seems that the Enabled property disable the content of the tabpage and not the access to the tabpage.
>
>Then I found http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskdisablingtabpagesprogrammatically.asp which is using the SelectedIndexChange to detect when a tab is selected and switch to another.
>
>There are 2 things that I don't like about this:
>1. There is a visual flicking. How can we stop the flicking?
>2. There is no visual clues that the tab is not enabled. How can we gray the caption of the tab page?
>
>Or if you have better ideas please tell me!


For anyone who could be interested, here is what I found!
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   
        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  '4
        Else
            e.Graphics.DrawString(Me.TabPages(e.Index).Text, e.Font, b, 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
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Previous
Reply
Map
View

Click here to load this message in the networking platform