Click here to Skip to main content
15,886,778 members
Articles / Programming Languages / Visual Basic

Switchcontrols (now with text!)

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
26 Apr 2012CPOL1 min read 31.4K   629   20   7
Use a TabControl without a TabStrip and a SwitchControl.

Introduction

I was looking for a tab control without a tabstrip. I couldn't find any so I created one by myself. And I created a switch control too because I wanted to switch the pages of the TabLessTabControl, so I used a UserControl and added the code I needed.

The Form and controls at runtime:

SwitchControls/Screenshot.png

Form and controls at design mode:

SwitchControls/ScreenshotDesign.png

Using the code

If you have rebuilt the solution, you can insert the controls from the Toolbox. You can set the TabLessTabControl property directly. When you set the property, the page label will be adjusted. When you add new tab pages to your TabLessTabControl, the switcher will not refresh itself.

You can refresh the Switcher by (re)building the solution.

Here is the (very short) TabLessTabControl class:

VB
Public NotInheritable Class TabLessTabControl
    Inherits TabControl

    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = &H1328 AndAlso Not DesignMode Then
            m.Result = New IntPtr(1)
        Else
            MyBase.WndProc(m)
        End If
    End Sub
End Class

The switcher class has just four controls:

VB
Me.FwButton = New System.Windows.Forms.Label()
Me.BckButton = New System.Windows.Forms.Label()
Me.StateLabel = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'FwButton
'
Me.FwButton.BackColor = System.Drawing.Color.White
Me.FwButton.Dock = System.Windows.Forms.DockStyle.Right
Me.FwButton.Font = New System.Drawing.Font("Wingdings 3", 12.0!, _
   System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(2, Byte))
Me.FwButton.ForeColor = System.Drawing.Color.Black
Me.FwButton.Location = New System.Drawing.Point(160, 0)
Me.FwButton.Name = "FwButton"
Me.FwButton.Size = New System.Drawing.Size(40, 40)
Me.FwButton.TabIndex = 3
Me.FwButton.Text = "u"
Me.FwButton.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'BckButton
'
Me.BckButton.BackColor = System.Drawing.Color.White
Me.BckButton.Dock = System.Windows.Forms.DockStyle.Left
Me.BckButton.Enabled = False
Me.BckButton.Font = New System.Drawing.Font("Wingdings 3", 12.0!, _
   System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(2, Byte))
Me.BckButton.ForeColor = System.Drawing.Color.Black
Me.BckButton.Location = New System.Drawing.Point(0, 0)
Me.BckButton.Name = "BckButton"
Me.BckButton.Size = New System.Drawing.Size(40, 40)
Me.BckButton.TabIndex = 4
Me.BckButton.Text = "t"
Me.BckButton.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'StateLabel
'
Me.StateLabel.BackColor = System.Drawing.Color.White
Me.StateLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.StateLabel.Dock = System.Windows.Forms.DockStyle.Fill
Me.StateLabel.ForeColor = System.Drawing.Color.Black
Me.StateLabel.Location = New System.Drawing.Point(40, 0)
Me.StateLabel.Name = "StateLabel"
Me.StateLabel.Size = New System.Drawing.Size(120, 40)
Me.StateLabel.TabIndex = 5
Me.StateLabel.Text = "Pagina 1 van 1"
Me.StateLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Switcher
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.Color.White
Me.Controls.Add(Me.StateLabel)
Me.Controls.Add(Me.BckButton)
Me.Controls.Add(Me.FwButton)
Me.MinimumSize = New System.Drawing.Size(200, 40)
Me.Name = "Switcher"
Me.Size = New System.Drawing.Size(200, 40)
Me.ResumeLayout(False)

The workspace of the switching control is quite easy as the following code shows:

VB
#Region "WorkSpace"
    Private Sub SW_LOAD() Handles MyBase.Load
        AdJustStateLabel()
    End Sub
    Public DispWorker As Integer = 1
    Public Property TabLessTabControl() As TabLessTabControl
        Get
            Return TabLessTabControl_
        End Get
        Set(value As TabLessTabControl)
            TabLessTabControl_ = value
            AdJustStateLabel()
            Me.Invalidate()
        End Set
    End Property

    'Public Property SelectedIndex As Integer
    '    Get
    '        'Return TabLessTabControl_.SelectedIndex
    '    End Get
    '    Set(ByVal value As Integer)
    '        If value > TabLessTabControl_.TabCount Then
    '            Exit Property
    '        ElseIf value < 0 Then
    '            Exit Property
    '        Else
    '            TabLessTabControl_.SelectedIndex = value
    '        End If
    '    End Set
    'End Property 

    Private Sub AdJustStateLabel()
        If Langu = Lang.English Then
            StateLabel.Text = "Page " & DispWorker & " of " & TabLessTabControl_.TabCount
        ElseIf Langu = Lang.Française Then
            StateLabel.Text = "Page " & DispWorker & " de " & TabLessTabControl_.TabCount
        ElseIf Langu = Lang.Español Then
            StateLabel.Text = "Página " & DispWorker & " de " & TabLessTabControl_.TabCount
        ElseIf Langu = Lang.Deutsch Then
            StateLabel.Text = "Seite " & DispWorker & " von " & TabLessTabControl_.TabCount
        ElseIf Langu = Lang.Nederlands Then
            StateLabel.Text = "Pagina " & DispWorker & " van " & TabLessTabControl_.TabCount
        End If
    End Sub

    Private Sub BckBtnClick(sender As Object, e As EventArgs) Handles BckButton.Click
        TabLessTabControl_.SelectedIndex -= 1
        DispWorker = TabLessTabControl_.SelectedIndex + 1
        If TabLessTabControl_.SelectedIndex = 0 Then
            BckButton.Enabled = False
        Else
            BckButton.Enabled = True
        End If
        FwButton.Enabled = True
        AdJustStateLabel()
    End Sub

    Private Sub FwBtnClick(ByVal sender As Object, ByVal e As EventArgs) Handles FwButton.Click
        TabLessTabControl_.SelectedIndex += 1
        DispWorker = TabLessTabControl_.SelectedIndex + 1
        If TabLessTabControl_.SelectedIndex = TabLessTabControl_.TabCount - 1 Then
            FwButton.Enabled = False
        Else
            FwButton.Enabled = True
        End If
        BckButton.Enabled = True
        AdJustStateLabel()
    End Sub
#End Region

Text version

I have created a text version too. You can turn the animation off.

SwitchControls/ScreenCapture_24-2-201221.07.58.gif

Points of interest

I liked programming, and at first, I didn't understand how a class could appear in the toolbox. But when I understood it, it was very funny, because I had many ideas to create. The TabLessTabControl property didn't work at the beginning, but when I saw an example it worked! Now I'm very interested at user-created components.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey19-Mar-12 19:37
professionalManoj Kumar Choubey19-Mar-12 19:37 
BugImportant Animation Bug!!! Pin
Dotnetifier25-Feb-12 7:09
Dotnetifier25-Feb-12 7:09 
GeneralRe: Important Animation Bug!!! Pin
Richard MacCutchan27-Feb-12 6:30
mveRichard MacCutchan27-Feb-12 6:30 
QuestionWhy the large screengrab....... Pin
DaveAuld19-Nov-11 21:54
professionalDaveAuld19-Nov-11 21:54 
AnswerRe: Why the large screengrab....... Pin
Dotnetifier24-Feb-12 9:39
Dotnetifier24-Feb-12 9:39 
GeneralNeed more work Pin
Kunal Chowdhury «IN»19-Nov-11 15:40
professionalKunal Chowdhury «IN»19-Nov-11 15:40 
GeneralRe: Need more work Pin
thatraja19-Nov-11 22:57
professionalthatraja19-Nov-11 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.