[C#] TabPage eines TabControls ein-/ausblenden

Standardmäßig gibt es kein Hide() oder Visible = true/false für TabPages. Man muss sich eines Tricks bedienen, bei dem die TabPages aus der internen Liste des TabControls gelöscht bzw. wieder hinzugefügt werden.

// TabPage "TabPage1", die zur Designzeit erstellt wurde, ausblenden
TabControl1.TabPages.Remove(TabPage1);

// TabPage "TabPage1", die zur Designzeit erstellt wurde, wieder am Anfang der Tabliste hinzufügen
TabControl1.TabPages.Add(TabPage1);

// TabPage "TabPage1", die zur Designzeit erstellt wurde, an zweiter Stelle in die Tabliste einfügen
TabControl1.TabPages.Insert(1, TabPage1);

Im Folgenden wird eine Klasse beschrieben, die automatisiert per Index immer nur eine bestimmte TabPage eines TabControls in Form eines schrittweise durchschaltbaren, geführten Einstellungsassistenten anzeigt. Am Ende wird ein Startbutton freigeschaltet.
Aufruf-Beispiel:

// alle zur Designzeit erstellten TabPages von TabControl1 werden verwaltet
// die Buttons btnPrevious und btnNext (müssen nicht angegeben werden) schalten die TabPages um
// der Button btnStart wird auf der letzten Seite freigeschaltet.
// "<-", "->" sind strings, die innerhalb der Buttons zusätzlich zum Namen angezeigt werden
// true == TabPage.Text + [1/x] anzeigen
CTabSelector _TabSelector = new CTabSelector(TabControl1, btnPrevious, btnNext, btnStart, "<-", "->", true);

// schaltet die TabPages zurück
private void btnBkwd_Click(object sender, EventArgs e)
{
    if (_TabSelector != null)
    {
        _TabSelector.CurrentTabIndex--;
    }
}
// schaltet die TabPages vor
private void btnFwd_Click(object sender, EventArgs e)
{
    if (_TabSelector != null)
    {
        _TabSelector.CurrentTabIndex++;
    }
}

Code:

/// <summary>
/// helper class for showing/hiding tabs in a TabControl
/// freeware (W) 2009 by SiteAdmin of www.codezentrale.6x.to
/// Usage:
/// private TabSelector _TabSelector = new TabSelector(TabControl1, btnPrevious, btnNext, btnStart, "<-", "->", true);
/// 
/// private void btnPrevious_Click(object sender, EventArgs e)
/// {
///    if (_TabSelector != null)
///    {
///        _TabSelector.CurrentTabIndex--;
///    }
/// }
/// private void btnNext_Click(object sender, EventArgs e)
/// {
///    if (_TabSelector != null)
///    {
///       _TabSelector.CurrentTabIndex++;
///    }
/// }
/// </summary>
class TabSelector
{
	#region vars
	private TabControl _tctrlTabControl = null;
	private int _iCurrentTabIndex = -1;
	private List<TabPage> _TabPages = new List<TabPage>();
	private List<string> _TabCaptions = new List<string>();
	private List<bool> _TabPageHidden = new List<bool>();
	private Button _btnPrevious = null;
	private Button _btnNext = null;
	private Button _btnFinal = null;
	private string _sPreviousItem = string.Empty;
	private string _sNextItem = string.Empty;
	private bool _bShowProgress = false;
	#endregion

	#region properties
	/// <summary>
	/// switches current shown tab by index, e.g. CurrentTabIndex++, CurrentTabIndex--
	/// </summary>
	public int SetCurrentTabIndex
	{
		get { return _iCurrentTabIndex; }
		set
		{
			if (_tctrlTabControl != null)
			{
				int iCurrentValue = value;

				// forwards
				if (iCurrentValue > _iCurrentTabIndex)
				{
					while (_TabPageHidden[iCurrentValue])
					{
						iCurrentValue++;
					}
				}
				else
					// backwards
					if (iCurrentValue < _iCurrentTabIndex)
					{
						while (_TabPageHidden[iCurrentValue])
						{
							iCurrentValue--;
						}
					}

				if (iCurrentValue >= 0 && iCurrentValue < _TabPages.Count)
				{
					_iCurrentTabIndex = iCurrentValue;
				}

				this.RefreshTabControl();
			}
		}
	}

	/// <summary>
	/// refreshes the current tabcontrol
	/// </summary>
	private void RefreshTabControl()
	{
		_tctrlTabControl.TabPages.Clear();

		int iShownTabPages = _TabPages.Count;

		for (int i = 0; i < _TabPages.Count; i++)
		{
			if (_TabPageHidden[i])
			{
				iShownTabPages--;
			}
		}

		int iHiddenTabPages = 0;

		for (int i = 0; i < _iCurrentTabIndex; i++)
		{
			if (_TabPageHidden[i])
			{
				iHiddenTabPages++;
			}
		}

		_TabPages[_iCurrentTabIndex].Text = _bShowProgress ? _TabCaptions[_iCurrentTabIndex] + " [" + (_iCurrentTabIndex + 1 - iHiddenTabPages).ToString() + "/" + iShownTabPages.ToString() + "]" : _TabCaptions[_iCurrentTabIndex];
		_tctrlTabControl.TabPages.Add(_TabPages[_iCurrentTabIndex]);

		if (_btnPrevious != null)
		{
			if ((_iCurrentTabIndex > 0) && (_iCurrentTabIndex < _TabPages.Count))
			{
				int iPrevIndex = _iCurrentTabIndex - 1;

				while (_TabPageHidden[iPrevIndex] && (iPrevIndex > 0))
				{
					iPrevIndex--;
				}

				_btnPrevious.Text = _sPreviousItem + _TabCaptions[iPrevIndex];
				_btnPrevious.Visible = true;
			}
			else
			{
				_btnPrevious.Text = string.Empty;
				_btnPrevious.Visible = false;
			}
		}

		if (_btnNext != null)
		{
			if ((_iCurrentTabIndex >= 0) && (_iCurrentTabIndex < _TabPages.Count - 1))
			{
				int iNextIndex = _iCurrentTabIndex + 1;

				while (_TabPageHidden[iNextIndex] && (iNextIndex < _TabPages.Count))
				{
					iNextIndex++;
				}

				_btnNext.Text = _TabCaptions[iNextIndex] + _sNextItem;
				_btnNext.Visible = true;
			}
			else
			{
				_btnNext.Text = string.Empty;
				_btnNext.Visible = false;
			}
		}

		_btnFinal.Visible = this.IsLastPage;
	}

	/// <summary>
	/// returns the name of the current previous tab
	/// </summary>
	public string PreviousTabPage
	{
		get
		{
			string sRetVal = string.Empty;

			if (_tctrlTabControl != null)
			{
				if (_iCurrentTabIndex > 0 && _iCurrentTabIndex < _TabPages.Count)
				{
					sRetVal = _TabCaptions[_iCurrentTabIndex - 1];
				}
			}

			return sRetVal;
		}
	}

	/// <summary>
	/// returns the name of the current next tab
	/// </summary>
	public string NextTabPage
	{
		get
		{
			string sRetVal = string.Empty;

			if (_tctrlTabControl != null)
			{
				if (_iCurrentTabIndex >= 0 && _iCurrentTabIndex < _TabPages.Count - 1)
				{
					sRetVal = _TabCaptions[_iCurrentTabIndex + 1];
				}
			}

			return sRetVal;
		}
	}

	/// <summary>
	/// is the current shown page the first
	/// </summary>
	public bool IsFirstPage
	{
		get { return (_iCurrentTabIndex == 0); }
	}

	/// <summary>
	/// is the current shown page the last
	/// </summary>
	public bool IsLastPage
	{
		get { return (_iCurrentTabIndex == _TabPages.Count - 1); }
	}
	#endregion

	/// <summary>
	/// constructor of tabselector object
	/// </summary>
	/// <param name="tctrlTabControl">a TabControl whose tabs should be switched</param>
	/// <param name="btnPrevious">a button that switches the tabs backwards, could also be null</param>
	/// <param name="btnNext">a button that switches the tabs forwards, could also be null</param>
	/// <param name="btnFinal">a button that is shown at the end</param>
	/// <param name="sPreviousItem">string that should be added before every tabstring that is shown in btnPrevious, could also be empty</param>
	/// <param name="sNextItem">string that should be added after every tabstring that is shown in btnNext, could also be empty</param>
	/// <param name="bShowProgress">show current page number every tab</param>
	public TabSelector(TabControl tctrlTabControl, Button btnPrevious, Button btnNext, Button btnFinal, string sPreviousItem, string sNextItem, bool bShowProgress)
	{
		_tctrlTabControl = tctrlTabControl;
		_btnFinal = btnFinal;
		_btnFinal.Visible = false;

		if (_tctrlTabControl != null)
		{
			for (int i = 0; i < _tctrlTabControl.TabPages.Count; i++)
			{
				_TabPages.Add(_tctrlTabControl.TabPages[i]);
				_TabCaptions.Add(_tctrlTabControl.TabPages[i].Text);
				_TabPageHidden.Add(false);
			}

			if (_tctrlTabControl.TabPages.Count > 0)
			{
				_bShowProgress = bShowProgress;

				_tctrlTabControl.TabPages.Clear();
				_tctrlTabControl.TabPages.Add(_TabPages[0]);

				if (btnPrevious != null)
				{
					_btnPrevious = btnPrevious;
					if (sPreviousItem != string.Empty)
					{
						_sPreviousItem = sPreviousItem + " ";
					}
				}

				if (btnNext != null)
				{
					_btnNext = btnNext;
					if (sNextItem != string.Empty)
					{
						_sNextItem = " " + sNextItem;
					}
				}

				this.SetCurrentTabIndex = 0;
			}
		}
	}
	/// <summary>
	/// hide a given tabpage
	/// </summary>
	/// <param name="Page">the tabpage to hide</param>
	public void HideTabPage(TabPage Page)
	{
		int iFoundIndex = _TabPages.IndexOf(Page);

		if (iFoundIndex >= 0)
		{
			_TabPageHidden[iFoundIndex] = true;

			this.RefreshTabControl();
		}
	}
	/// <summary>
	/// show again a given tabpage
	/// </summary>
	/// <param name="Page">the tabpage to show again</param>
	public void ShowTabPage(TabPage Page)
	{
		int iFoundIndex = _TabPages.IndexOf(Page);

		if (iFoundIndex >= 0)
		{
			_TabPageHidden[iFoundIndex] = false;

			this.RefreshTabControl();
		}
	}
}