11 December, 2011

Free alternative to tab controls [VB.net]

The first thing you will need is a UI. Seeing as how we are building a 'tab control' you will need the following.
  • Button - This is going to act as our 'tab' so to speak.
  • Panel - These are where we are going to put all of our content.

To start, decide how many tabs you will need. In this tutorial, I will create only 5. So my form, so far, looks like so:

[Image: XSwMXFQhHnQAELnTBhqE.PNG]

You can see that I have the five tabs laid out, and each panel is the same size but the panels are spread across the form. This is only temporary and makes creating content easier.

Next we will need a little snippet to make choosing a panel/tab easier. You could use show/hide on each one depending on which button you click, but that takes a LOT of space in your program and I, for one, like creating in as little code as possible. The snippet is below.

Code:
Private p As Panel
    Private Sub currentTab(ByVal c As Panel)
        Try
            If Not p Is Nothing Then  'If panel is not nothing...
                p.SendToBack()  'Send it to back...
                p.Visible = False  'and make it invisible.
                p = Nothing  'Then set it to nothing
            End If
            p = c  'Set panel as the panel you chose...
            p.Visible = True  'and make it visible...
            p.BringToFront()  'then bring it to front.
        Catch
        End Try
    End Sub

Usage is really simple.
Code:
currentTab(Your panel name here)

What this is going to do is bring the panel you tell it to to the front of your form, with all others behind it. Add that snippet to your project, then add the usage code above to each button that you created to be a part of the tab control.

The rest is really easy. Lay out your content in each tab and make sure you like it. Move all but the first tabs away from your work area and pick a spot for the tab control to go. Then copy the location of your first panel and paste it into each other panel. This will lay them on top of each other without any one parenting any other. Make sure they are the same size and you have a beautiful tab control that is just as good as any paid method.

For you visual learners, this is what my finished project looked like.
[Image: BqlxYVjNoUEqgUsqlwtI.png]

I may post a gif of it working a bit later, but this should suffice for now.
If anyone needs help, PM me. I am always happy to.
And again, sorry for the random snippet post.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home