+1 for microsoft :-) .
I managed to do it this way:
(it assumes you have a Next button that displays the next TabPage - tabSteps is the name of the Tab control)
At start up, save all the tabpages in a proper list.
When user presses Next button, remove all the TabPages in the tab control, then add that with the proper index:
int step = -1;
List<TabPage> savedTabPages;
private void FMain_Load(object sender, EventArgs e) {
// save all tabpages in the list
savedTabPages = new List<TabPage>();
foreach (TabPage tp in tabSteps.TabPages) {
savedTabPages.Add(tp);
}
SelectNextStep();
}
private void SelectNextStep() {
step++;
// remove all tabs
for (int i = tabSteps.TabPages.Count - 1; i >= 0 ; i--) {
tabSteps.TabPages.Remove(tabSteps.TabPages[i]);
}
// add required tab
tabSteps.TabPages.Add(savedTabPages[step]);
}
private void btnNext_Click(object sender, EventArgs e) {
SelectNextStep();
}
Update
public class TabControlHelper {
private TabControl tc;
private List<TabPage> pages;
public TabControlHelper(TabControl tabControl) {
tc = tabControl;
pages = new List<TabPage>();
foreach (TabPage p in tc.TabPages) {
pages.Add(p);
}
}
public void HideAllPages() {
foreach(TabPage p in pages) {
tc.TabPages.Remove(p);
}
}
public void ShowAllPages() {
foreach (TabPage p in pages) {
tc.TabPages.Add(p);
}
}
public void HidePage(TabPage tp) {
tc.TabPages.Remove(tp);
}
public void ShowPage(TabPage tp) {
tc.TabPages.Add(tp);
}
}
As a cheap work around, I've used a label to cover up the tabs I wanted to hide.
We can then use the visible prop of the label as a substitute. If anyone does go this route, don't forget to handle keyboard strokes or visibility events. You wouldn't want the left right cursor keys exposing the tab you're trying to hide.
I also had this question. tabPage.Visible is not implemented as stated earlier, which was a great help (+1). I found you can override the control and this will work. A bit of necroposting, but I thought to post my solution here for others...
[System.ComponentModel.DesignerCategory("Code")]
public class MyTabPage : TabPage
{
private TabControl _parent;
private bool _isVisible;
private int _index;
public new bool Visible
{
get { return _isVisible; }
set
{
if (_parent == null) _parent = this.Parent as TabControl;
if (_parent == null) return;
if (_index < 0) _index = _parent.TabPages.IndexOf(this);
if (value && !_parent.TabPages.Contains(this))
{
if (_index > 0) _parent.TabPages.Insert(_index, this);
else _parent.TabPages.Add(this);
}
else if (!value && _parent.TabPages.Contains(this)) _parent.TabPages.Remove(this);
_isVisible = value;
base.Visible = value;
}
}
protected override void InitLayout()
{
base.InitLayout();
_parent = Parent as TabControl;
}
}
I've used the same approach but the problem is that when tab page was removed from the tab control TabPages list, it is removed from the tab page Controls list also. And it is not disposed when form is disposed.
+1 for microsoft :-) .
I managed to do it this way:
(it assumes you have a
Next
button that displays the next TabPage -tabSteps
is the name of the Tab control)At start up, save all the tabpages in a proper list.
When user presses
Next
button, remove all the TabPages in the tab control, then add that with the proper index:Update
As a cheap work around, I've used a label to cover up the tabs I wanted to hide.
We can then use the visible prop of the label as a substitute. If anyone does go this route, don't forget to handle keyboard strokes or visibility events. You wouldn't want the left right cursor keys exposing the tab you're trying to hide.
I also had this question. tabPage.Visible is not implemented as stated earlier, which was a great help (+1). I found you can override the control and this will work. A bit of necroposting, but I thought to post my solution here for others...
I've used the same approach but the problem is that when tab page was removed from the tab control TabPages list, it is removed from the tab page Controls list also. And it is not disposed when form is disposed.
So if you have a lot of such "hidden" tab pages, you can get windows handle quota exceeded error and only application restart will fix it.
Variant 1
In order to avoid visual klikering you might need to use:
or
Remove a tab page:
Add a tab page:
Insert a tab page at specific location:
Do not forget to revers the changes:
or
Variant 2
you can set the parent of the tabpage to null for hiding and to show just set tabpage parent to the tabcontrol