Click here to Skip to main content
Click here to Skip to main content
Alternative Article

Magic TabControl - VS.NET Style

, 4 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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);

...
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...
         _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.
 
 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.

 
 

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)

Share

About the Author

L3CodeProject
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

 
Questiondownload works for me... PinmemberL3CodeProject8-Dec-14 12:31 
GeneralMy vote of 1 Pinmemberfredatcodeproject29-Oct-14 23:58 
QuestionDownload still broken... Pinmemberpt140112-Sep-14 11:59 
AnswerRe: Download still broken... Pinmemberfredatcodeproject29-Oct-14 23:56 
Newsdownload link is still broken Pinmemberginocic17-Aug-14 2:09 
QuestionDownload link is broken Pinmemberfreakyit4-Jul-14 0:50 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 4 Jul 2014
Article Copyright 2014 by L3CodeProject
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid