Click here to Skip to main content
14,357,666 members

Magic TabControl - VS.NET Style

Rate this:
3.44 (6 votes)
Please Sign up or sign in to vote.
3.44 (6 votes)
4 Jul 2014CPOL
This is an alternative for "Magic TabControl - VS.NET Style"


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


This is an update to the Magic TabControl

Using the code

Image 1

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);

To the AddTabPage function the VisibleChanged event needed to be captured in order to allow a given page to be seen or not to be seen.  The OnPageVisibleChanged event will now handle how the tab pages are to be handled when the Visible status changes.  In this case, a seperate collection has been created to hold all tab pages set to Visible = false (_tabPagesInVisible).  When the Visible = true, the tab page will be searched from the _tabPagesInVisible collection removed and placed back into the _tabPages collection.
/// <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...
         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);
Now, since we still want to know what tab pages are truly a part of the tab page control, a mod to the tabPages property was also made, to merge the two data collections.
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;
The handling of the page.Enabled was a more simple process, again it affected code in the TabControl.cs module
  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.



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



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader
United States United States
Working with computers and code since the days of the Fluke 1720A touch control system (1979). A lot of peeks and pokes to get the assembly code in..
Created firmware compilers and optimizers...
These days, life is less complex programming in ASP.NET. Laughing at the arguments of VB.NET vs C#...hmmm, both interpretive, both dependent on the same core...(would hate for those lads who don't get work on the projects that I have prior to ASP.NET)

Comments and Discussions

GeneralCode has history..see phil wright Magic TabControl from 2002 Pin
Zipadie Doodah20-Jan-16 2:09
memberZipadie Doodah20-Jan-16 2:09 
QuestionThis article has a poor background section Pin
JaredThirsk15-May-15 12:01
memberJaredThirsk15-May-15 12:01 
AnswerRe: This article has a poor background section Pin
Garth J Lancaster15-May-15 13:02
professionalGarth J Lancaster15-May-15 13:02 
GeneralRe: This article has a poor background section Pin
Zipadie Doodah20-Jan-16 2:00
memberZipadie Doodah20-Jan-16 2:00 
QuestionDownload is broken Pin
Jaime_Des27-Feb-15 1:40
memberJaime_Des27-Feb-15 1:40 
GeneralMy vote of 3 Pin
fredatcodeproject16-Feb-15 3:20
professionalfredatcodeproject16-Feb-15 3:20 
QuestionHmmm - Download still doesn't work. Pin
Member 12077898-Feb-15 17:50
memberMember 12077898-Feb-15 17:50 
Questiondownload works for me... Pin
L3CodeProject8-Dec-14 12:31
memberL3CodeProject8-Dec-14 12:31 
AnswerRe: download works for me... Pin
fredatcodeproject16-Feb-15 3:19
professionalfredatcodeproject16-Feb-15 3:19 
GeneralMy vote of 1 Pin
fredatcodeproject29-Oct-14 23:58
professionalfredatcodeproject29-Oct-14 23:58 
QuestionDownload still broken... Pin
pt140112-Sep-14 11:59
memberpt140112-Sep-14 11:59 
AnswerRe: Download still broken... Pin
fredatcodeproject29-Oct-14 23:56
professionalfredatcodeproject29-Oct-14 23:56 
Newsdownload link is still broken Pin
ginocic17-Aug-14 2:09
memberginocic17-Aug-14 2:09 
QuestionDownload link is broken Pin
freakyit4-Jul-14 0:50
memberfreakyit4-Jul-14 0:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 4 Jul 2014


10 bookmarked