C# WinForms - Janus UITab Control Extension Method - RestoreTabs after tab closed or container resized

Restore Janus UITabPages after a tab is closed or the control is resized. Hide the navigation tool if that's applicable. Use C# Extension Methods to do that.

 Recently I was asked to resolve an issue with Janus Systems UITab Control.

 

The Problem

Whenever the user opens too many tabs some of the tabs are "pushed" over the visible area and are hidden from the user. A couple of navigation arrows are made available and the user can "browse" between the open tabs.

 

Now, when the user closes a tab and there's enough space for hidden tabs to return into view one would expect that the tab would refresh itself, hide the navigation arrows if that's applicable and will be more dynamic. But what do you know it doesn't ...

 

The Solution

In order to fix the issue and add this desired behavior to the UITab I looked into C# Extension Methods, in short, it enable you to extend an existing control or an object without inheriting it and committing to all it's interfaces.

 

I've added my own UITabExtensionMethods class and implemented a quick and elegant solution.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Janus.Windows.UI.Tab;

namespace UITabExt {
    /// <summary>
    /// Extend Janus UITab Control
    /// </summary>
    public static class UITabExtensionMethods {

        /// <summary>
        /// This extension method takes care of restoring the displayed tab whenever
        /// a tab is closed or the UITab control is resized.
        /// This method should be invoked whenever "<b>uiTab1_SelectedTabChanged</b>"
        /// or "<b>uiTab1_Resize</b>" events are fired/
        /// This method tries to find the newly/selected tab, preserve it.
        /// The it will check if there are any TabPages to show.
        /// If there are it will invoke the EnusreVisible call twice,
        /// the first time is the the 1st tab (index = 0) and then the 2nd
        /// time is for the preserved tab.
        /// This will ensure us that whenever a TabPage is closed or the UITab is resized
        /// the tab strip (if it is shown) will display all the tabs that should be visible
        /// </summary>
        /// <param name="_uiTab"></param>
        public static void RestoreTabs(this UITab _uiTab) {
            // Ensure that our UITab object is initalized, if not exit gracefully.
            if (_uiTab == null) {
                return;
            }
   
            // Ensure that the parent form exists and that it is not minimized, if
            // so do nothing and exit
            From from = _uiTab.FindForm();

            if (form == null || form.WindowState == FormWindowState.Minimized)
            {
               return;
            }

            // If there are no tabs open, do nothing
            if (_uiTab.TabPages.Count > 0) {
                // Define a local TabPage object to preserve the selected TabPage
                UITabPage selectedTabPage = _uiTab.SelectedTab;

                // First show the first tab
                _uiTab.EnsureVisible(_uiTab.TabPages[0]);

                // If a selected TabPage exists (not null)
                // Check if the selected tab is the first tab (index = 0),
                // if so, do nothing, otherwise show it
                if (selectedTabPage != null) {
                    if (selectedTabPage.Index > 0) {
                        _uiTab.EnsureVisible(selectedTabPage);
                    }
                }
            }
        }
    }
}

 

This solution works as it uses the control's own refresh mechanism.

 

In order to invoke this extension method I've hooked into two event handlers for my UITab control, see below:

 

        #region UITab Event Handlers

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTab1_SelectedTabChanged(object sender, Janus.Windows.UI.Tab.TabEventArgs e) {
            uiTab1.RestoreTabs();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiTab1_Resize(object sender, EventArgs e) {
            uiTab1.RestoreTabs();
        }

        #endregion

 

Enjoy

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com