Magic TabControl - VS.NET Style






3.44/5 (6 votes)
This is an alternative for "Magic TabControl - VS.NET Style"
Introduction
Update to the Magic TabControl to add the ability to allow:
- Change the Tab Title Font
- Change Enable property of a Tab page
- Change Visible property of a Tab page
Background
This is an update to the Magic TabControl
Using the code
In this release of the Magic TabControl, the ability to make Visible a given page as well as Enable a given page is now implemented. The programmer can set the status programmatically as shown in the code sample below.
private void chkVisible_CheckedChanged(object sender, EventArgs e)
{
if (cboTabsInControl.SelectedIndex > -1)
{
((Crownwood.Magic.Controls.TabPage)cboTabsInControl.SelectedItem).Visible = chkVisible.Checked;
}
}
private void chkEnabled_CheckedChanged(object sender, EventArgs e)
{
if (cboTabsInControl.SelectedIndex >-1)
{
((Crownwood.Magic.Controls.TabPage)cboTabsInControl.SelectedItem).Enabled = chkEnabled.Checked;
}
}
Points of Interest
The ability to add this simple mods was straight forward. For the Visibile property, the following enhancement to TabControls.cs was made:
protected virtual void AddTabPage(TabPage page)
{
// Has not been shown for the first time yet
page.Shown = false;
page.VisibleChanged += new EventHandler(OnPageVisibleChanged);
...
/// <summary>
/// mod: <c>OnPageVisiblieChanged</c>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnPageVisibleChanged(object sender,EventArgs e)
{
if (sender is TabPage)
{
if (!((TabPage)sender).Visible)
{
((TabPage)sender).TabIndex = _tabPages.IndexOf((TabPage)sender); // Remember where it was...
_tabPages.Remove((TabPage)sender);
_tabPagesInVisible.Add((TabPage)sender);
}
else
{
if (_tabPagesInVisible.Count != 0)
{
foreach(TabPage page in _tabPagesInVisible)
{
if (page.Title == ((TabPage)sender).Title)
{
//_tabPages.Add((TabPage)sender); // ToDo: Need to get the page back to its original position.
_tabPages.Insert(((TabPage)sender).TabIndex, (TabPage)sender);
_tabPagesInVisible.Remove(page);
break;
}
}
}
}
}
}
public virtual TabPageCollection TabPages
{
get {
//Mod: Combine the non visible with the visible
TabPageCollection allPages = _tabPages;
if (_tabPagesInVisible.Count != 0)
{
foreach(TabPage page in _tabPagesInVisible)
{
allPages.Insert(page.TabIndex, page); // Make sure to put the pages back where they were originally.
}
}
return allPages; // _tabPages;
}
}
protected virtual void MovePageSelection(TabPage page)
{
int pageIndex = _tabPages.IndexOf(page);
if (pageIndex != _pageSelected && page.Enabled == true)
...
With these simple updates, one can now prevent the user from going into a given tab or simple done't make that tab(s) visible to the user if they do not have rights to access that data.
History
Update to the Magic TabControl to add the ability to allow:
- Change the Tab Title Font
- Change Enable property of a Tab page
- Change Visible property of a Tab page