TabControl Question

R

Richard

Hi,

Simple tabcontrol question:

I have a tab control with a number of tabpages on it. What I want is to
show a particular tabpage when I click a button that is not on the
tabcontrol. Can anyone tell me how I would code this?

Thanks
Richard
 
G

Greg Burns

Here is some code I have that does that.

I have four checkboxs across the top (appearance properties = Button). When
you check/depress the button, it shows corresponding Tabpage. When no
buttons are pressed, TabControl is hidden:

Good luck,
Greg


' class level variables
Private _tabs(3) As TabPage

' the names of my 4 tabs...
Enum TabNames
Changes
Transfer
Leave
Term
End Enum


Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call


TabControl1.TabPages.Clear()
TabControl1.Visible = False ' hide by default
End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles CheckBox1.CheckedChanged

ShowHideTab(TabNames.Changes)

' light the button up when pressed...
If CheckBox1.CheckState = CheckState.Checked Then
CheckBox1.ForeColor = Color.White
CheckBox1.BackColor = Color.Green
CheckBox1.Font = New Font(Me.Font.Name, Me.Font.Size,
FontStyle.Bold)
Else
CheckBox1.ForeColor = SystemColors.ControlText
CheckBox1.BackColor = SystemColors.Control
CheckBox1.Font = Nothing
End If

End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

ShowHideTab(TabNames.Transfer)

If CheckBox2.CheckState = CheckState.Checked Then
CheckBox2.ForeColor = Color.White
CheckBox2.BackColor = Color.Green
CheckBox2.Font = New Font(Me.Font.Name, Me.Font.Size,
FontStyle.Bold)

Else
CheckBox2.ForeColor = SystemColors.ControlText
CheckBox2.BackColor = SystemColors.Control
CheckBox2.Font = Nothing

End If


End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged


ShowHideTab(TabNames.Leave)

If CheckBox3.CheckState = CheckState.Checked Then
CheckBox3.ForeColor = Color.White
CheckBox3.BackColor = Color.Green
CheckBox3.Font = New Font(Me.Font.Name, Me.Font.Size,
FontStyle.Bold)

Else
CheckBox3.ForeColor = SystemColors.ControlText
CheckBox3.BackColor = SystemColors.Control
CheckBox3.Font = Nothing

End If


End Sub

Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged

ShowHideTab(TabNames.Term)

If CheckBox4.CheckState = CheckState.Checked Then
CheckBox4.ForeColor = Color.White
CheckBox4.BackColor = Color.Green
CheckBox4.Font = New Font(Me.Font.Name, Me.Font.Size,
FontStyle.Bold)

Else
CheckBox4.ForeColor = SystemColors.ControlText
CheckBox4.BackColor = SystemColors.Control
CheckBox4.Font = Nothing

End If

End Sub

Private Sub ShowHideTab(ByVal i As TabNames)

If _tabs(i) Is Nothing Then
Me.Cursor = Cursors.WaitCursor

TabControl1.Visible = True
Select Case i
Case TabNames.Changes

_tabs(TabNames.Changes) = TabPage1
SortTabPages(TabPage1)

Case TabNames.Transfer

_tabs(TabNames.Transfer) = TabPage2
SortTabPages(TabPage2)


Case TabNames.Leave

_tabs(TabNames.Leave) = TabPage3
SortTabPages(TabPage3)

Case TabNames.Term

_tabs(TabNames.Term) = TabPage4
SortTabPages(TabPage4)

End Select

Me.Cursor = Cursors.Default
Else
_tabs(i) = Nothing
Select Case i
Case TabNames.Changes
TabControl1.TabPages.Remove(TabPage1)
Case TabNames.Transfer
TabControl1.TabPages.Remove(TabPage2)
Case TabNames.Leave
TabControl1.TabPages.Remove(TabPage3)
Case TabNames.Term
TabControl1.TabPages.Remove(TabPage4)
End Select

If TabControl1.TabCount = 0 Then
TabControl1.Visible = False
End If
End If

End Sub

Private Sub SortTabPages(ByVal currentTab As TabPage)


' To reorder the tabs, clear the collection and add them again

TabControl1.TabPages.Clear()
For Each tab As TabPage In _tabs
If Not tab Is Nothing Then
TabControl1.TabPages.Add(tab)
End If
Next

TabControl1.SelectedTab = currentTab


End Sub
 
R

Richard

Thanks Greg, that was just what I wanted.

Greg Burns said:
Here is some code I have that does that.

I have four checkboxs across the top (appearance properties = Button). When
you check/depress the button, it shows corresponding Tabpage. When no
buttons are pressed, TabControl is hidden:

Good luck,
Greg


' class level variables
Private _tabs(3) As TabPage

' the names of my 4 tabs...
Enum TabNames
Changes
Transfer
Leave
Term
End Enum


Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call


TabControl1.TabPages.Clear()
TabControl1.Visible = False ' hide by default
End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles CheckBox1.CheckedChanged

ShowHideTab(TabNames.Changes)

' light the button up when pressed...
If CheckBox1.CheckState = CheckState.Checked Then
CheckBox1.ForeColor = Color.White
CheckBox1.BackColor = Color.Green
CheckBox1.Font = New Font(Me.Font.Name, Me.Font.Size,
FontStyle.Bold)
Else
CheckBox1.ForeColor = SystemColors.ControlText
CheckBox1.BackColor = SystemColors.Control
CheckBox1.Font = Nothing
End If

End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

ShowHideTab(TabNames.Transfer)

If CheckBox2.CheckState = CheckState.Checked Then
CheckBox2.ForeColor = Color.White
CheckBox2.BackColor = Color.Green
CheckBox2.Font = New Font(Me.Font.Name, Me.Font.Size,
FontStyle.Bold)

Else
CheckBox2.ForeColor = SystemColors.ControlText
CheckBox2.BackColor = SystemColors.Control
CheckBox2.Font = Nothing

End If


End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged


ShowHideTab(TabNames.Leave)

If CheckBox3.CheckState = CheckState.Checked Then
CheckBox3.ForeColor = Color.White
CheckBox3.BackColor = Color.Green
CheckBox3.Font = New Font(Me.Font.Name, Me.Font.Size,
FontStyle.Bold)

Else
CheckBox3.ForeColor = SystemColors.ControlText
CheckBox3.BackColor = SystemColors.Control
CheckBox3.Font = Nothing

End If


End Sub

Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged

ShowHideTab(TabNames.Term)

If CheckBox4.CheckState = CheckState.Checked Then
CheckBox4.ForeColor = Color.White
CheckBox4.BackColor = Color.Green
CheckBox4.Font = New Font(Me.Font.Name, Me.Font.Size,
FontStyle.Bold)

Else
CheckBox4.ForeColor = SystemColors.ControlText
CheckBox4.BackColor = SystemColors.Control
CheckBox4.Font = Nothing

End If

End Sub

Private Sub ShowHideTab(ByVal i As TabNames)

If _tabs(i) Is Nothing Then
Me.Cursor = Cursors.WaitCursor

TabControl1.Visible = True
Select Case i
Case TabNames.Changes

_tabs(TabNames.Changes) = TabPage1
SortTabPages(TabPage1)

Case TabNames.Transfer

_tabs(TabNames.Transfer) = TabPage2
SortTabPages(TabPage2)


Case TabNames.Leave

_tabs(TabNames.Leave) = TabPage3
SortTabPages(TabPage3)

Case TabNames.Term

_tabs(TabNames.Term) = TabPage4
SortTabPages(TabPage4)

End Select

Me.Cursor = Cursors.Default
Else
_tabs(i) = Nothing
Select Case i
Case TabNames.Changes
TabControl1.TabPages.Remove(TabPage1)
Case TabNames.Transfer
TabControl1.TabPages.Remove(TabPage2)
Case TabNames.Leave
TabControl1.TabPages.Remove(TabPage3)
Case TabNames.Term
TabControl1.TabPages.Remove(TabPage4)
End Select

If TabControl1.TabCount = 0 Then
TabControl1.Visible = False
End If
End If

End Sub

Private Sub SortTabPages(ByVal currentTab As TabPage)


' To reorder the tabs, clear the collection and add them again

TabControl1.TabPages.Clear()
For Each tab As TabPage In _tabs
If Not tab Is Nothing Then
TabControl1.TabPages.Add(tab)
End If
Next

TabControl1.SelectedTab = currentTab


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top