Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Magic TabControl - VS.NET Style

3.44/5 (6 votes)
3 Jul 2014CPOL2 min read 22.5K   551  
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)