NoviceProgrammer
NoviceProgrammer

Reputation: 9

VB.Net 2008 Buttons acts as Tab Controls

I am looking for a way to design things differently in my project. Instead of using TabControls I wish to use Buttons (Instead of pressing the tabs on the top I would like to press the Buttons on the left-side). These buttons when pressed they have their own Panel where each has their own respective content.

Select Case tabAdmin.SelectedIndex
            Case 0
                If txtCode_Patient.Text = "" Then
                    txtCode_Patient.Focus()
                Else
                    cmdAdminister.Focus()
                End If
            Case 1
                If txtD_Patient.Text = "" Then
                    txtD_Patient.Focus()
                Else
                    cmdRefresh.Focus()
                End If
            Case 2
                If txtI_Patient.Text = "" Then
                    txtI_Patient.Focus()
                Else
                    cmdI_CUser.Focus()
                End If
            Case 3
                If txtStat_CS.Text = "" Then
                    txtStat_CS.Focus()
                Else
                    cmdStat_Refresh.Focus()
                End If

        End Select

The code above is similar to what my project acts and it works with TabControls. I want to do a similar thing but this time, like I said before, pressing Buttons on the left-side. How can I do a similar thing ?

UPDATE:

I found a way for this one but now my concern is how do I make it look like one of its default button 3D look-alike?

Public Class Tab
    Inherits TabControl
    Private Property DoubledBuffered As Boolean

    Sub New()
        SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
        DoubledBuffered = True
        SizeMode = TabSizeMode.Fixed
        ItemSize = New Size(30, 110)
    End Sub

    Protected Overrides Sub CreateHandle()
        MyBase.CreateHandle()

        Alignment = TabAlignment.Left
    End Sub

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        Dim B As New Bitmap(Width, Height)
        Dim G As Graphics = Graphics.FromImage(B)

        G.Clear(Color.AliceBlue)

        For i = 0 To TabCount - 1
            Dim TabRectangle As Rectangle = GetTabRect(i)

            If i = SelectedIndex Then
                '//Selected
                G.FillRectangle(Brushes.DarkSlateGray, TabRectangle)
            Else
                '//Not Selected
                G.FillRectangle(Brushes.AntiqueWhite, TabRectangle)
            End If

            G.DrawString(TabPages(i).Text, Font, Brushes.Black, TabRectangle, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})

        Next

        e.Graphics.DrawImage(B.Clone, 0, 0)
        G.Dispose() : B.Dispose()
        MyBase.OnPaint(e)
    End Sub
End Class

Upvotes: 0

Views: 556

Answers (4)

Steve Pettifer
Steve Pettifer

Reputation: 2043

There is an Outlook-style side bar available on Code Project. It has a VB version as well as C# and although it's knocking on a bit now, you could always adapt this to look a bit nicer. I have used it in the past and it worked pretty well as I recall.

Upvotes: 1

tinstaafl
tinstaafl

Reputation: 6948

Use a common click event handler for the buttons. Store the relevant tabindex in the Tag property of the buttons. Then tabAdmin.SelectedIndex equals the tag of the clicked button cast as integer.

Upvotes: 0

Adam Bowles
Adam Bowles

Reputation: 42

You can set the selected tab via tabAdmin.SelectedIndex = 0 (or 1, 2, etc, but remember it is 0 based)

You may also set the tab by the tab's name via tabAdmin.SelectedTab = TabName

Upvotes: 0

Dai
Dai

Reputation: 155280

Use the CheckBox control instead of Button, but set Appearance = Button, that way it looks exactly like a button can remains in the "pressed" state when clicked.

To shift between content, put each of your sub-forms into their own UserControl instances, then host them within a Panel control, then switch the .Visibility property of each sub-form according to which CheckBox was clicked.

Upvotes: 2

Related Questions