Hiding Tabs

How do i make a tab invisible or hidden on a tab control?
[63 byte] By [wabbit] at [2007-12-16]
# 1
Simply remove the TabPage from the TabControl.TabPages collection. If you want to re-add it later, simply squirrel away a copy in a member variable of your Form (or elsewhere).
public class MainForm : Form {
private List<TabPage> m_tabs = new List<TabPage>();

protected override void OnLoad(EventArgs e) {
foreach(TabPage tab in mainTabControl.TabPages) {
m_tabs.Add(tab);
}
}

private void HideTab(int index) {
mainTabControl.TabPages.RemoveAt(index);
}

private void ShowTab(int tabIndex, int position) {
mainTabControl.TabPages.Insert(position, m_tabs[tabIndex]);
}

// additional code
}

N.B. You'll have to keep your indices straight between the List<TabPage> and TabControl.TabPages. You may want to consider manipulating the tabs by tab name and keeping them in a Dictionary<string,TabPage> instead.

JamesKovacs at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
thanks
wabbit at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

Thanks for the idea. Would it be possible to post a Visual Basic equivalent?

n2a1s at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
Equivalent VB code would look like:

Public Class MainForm
Inherits Form

Private Sub HideTab(ByVal index As Integer)
mainTabControl.TabPages.RemoveAt(index)
End Sub

Protected Overrides Sub OnLoad(ByVal e As EventArgs)
For Each tab As TabPage In mainTabControl.TabPages
m_tabs.Add(tab)
Next
End Sub

Private Sub ShowTab(ByVal tabIndex As Integer, ByVal position As Integer)
mainTabControl.TabPages.Insert(position, m_tabs(tabIndex))
End Sub

Private m_tabs As List(Of TabPage) = New List(Of TabPage)

' additional code
End Class

JamesKovacs at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
Many thanks for your prompt reply.
n2a1s at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...