Click here to Skip to main content
15,889,216 members
Articles / Web Development / HTML
Alternative
Article

Magic TabControl - VS.NET Style

Rate me:
Please Sign up or sign in to vote.
3.44/5 (6 votes)
3 Jul 2014CPOL2 min read 21.6K   535   10   15
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

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.

C#
 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:

C#
  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.
C#
/// <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;
                   }
                }
             }
          }
     }
}
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.
 
C#
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
 
C#
  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

 

License

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


Written By
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 it...to 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 1:09
Zipadie Doodah20-Jan-16 1:09 
QuestionThis article has a poor background section Pin
JaredThirsk15-May-15 11:01
JaredThirsk15-May-15 11:01 
What is the Magic TabControl?

I see no links in the intro or background. If this is an update to something, it should link to it.

I see no screenshot of any tab controls.

The article is tagged as HTML.

Does that mean it is an HTML tab control?

How did this get featured by CodeProject?
AnswerRe: This article has a poor background section Pin
Garth J Lancaster15-May-15 12:02
professionalGarth J Lancaster15-May-15 12:02 
GeneralRe: This article has a poor background section Pin
Zipadie Doodah20-Jan-16 1:00
Zipadie Doodah20-Jan-16 1:00 
QuestionDownload is broken Pin
Jaime_Des27-Feb-15 0:40
Jaime_Des27-Feb-15 0:40 
GeneralMy vote of 3 Pin
fredatcodeproject16-Feb-15 2:20
professionalfredatcodeproject16-Feb-15 2:20 
QuestionHmmm - Download still doesn't work. Pin
Member 12077898-Feb-15 16:50
Member 12077898-Feb-15 16:50 
Questiondownload works for me... Pin
L3CodeProject8-Dec-14 11:31
L3CodeProject8-Dec-14 11:31 
AnswerRe: download works for me... Pin
fredatcodeproject16-Feb-15 2:19
professionalfredatcodeproject16-Feb-15 2:19 
GeneralMy vote of 1 Pin
fredatcodeproject29-Oct-14 22:58
professionalfredatcodeproject29-Oct-14 22:58 
QuestionDownload still broken... Pin
pt140112-Sep-14 10:59
pt140112-Sep-14 10:59 
AnswerRe: Download still broken... Pin
fredatcodeproject29-Oct-14 22:56
professionalfredatcodeproject29-Oct-14 22:56 
Newsdownload link is still broken Pin
ginocic17-Aug-14 1:09
ginocic17-Aug-14 1:09 
QuestionDownload link is broken Pin
freakyit3-Jul-14 23:50
freakyit3-Jul-14 23: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.