display order on tabs in winforms

6 posts, 0 answers
  1. Carisch
    Carisch avatar
    25 posts
    Member since:
    Aug 2007

    Posted 17 Apr 2008 Link to this post

    I would like to change the display order of tabs in code at runtime.  Can you please provide an example in C#.
  2. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 17 Apr 2008 Link to this post

    Hi Brian,

    Thank you for the question.

    The first thing when sorting tabs in RadTabStrip is to choose a criteria. I have prepared an example, which sorts the tabs based on their Text property. I have created a custom class that implements the IComparable interface. Then I put all the tabs into the array, sorted them with the Array.Sort() function, and returned them one by one back into my RadTabStrip's items collection. Please review the code-block provided below as a reference:
     
    class ComparableTab : IComparable  
    {
        #region Properties  
     
        private TabItem tabItem;  
     
        public TabItem TabItem  
        {  
            get   
            {  
                return tabItem;   
            }  
        }  
        private string tabItemText;  
     
        public string Text  
        {  
            get   
            {   
                return tabItemText;   
            }  
        }
        #endregion  
     
        #region Constructors  
     
        public ComparableTab(TabItem tab)  
        {  
            this.tabItem = tab;  
            this.tabItemText = tab.Text;  
        }
        #endregion  
     
        #region IComparable Members  
     
        public int CompareTo(object obj)  
        {  
            if (obj is ComparableTab)  
            {  
                ComparableTab tabItem2 = (ComparableTab)obj;  
                return this.tabItemText.CompareTo(tabItem2.Text);  
            }  
            else 
            {  
                throw new ArgumentException("Object is not a ComparableTab!");  
            }  
        }
        #endregion  
     
    }  
     
    private void SortTabs(RadTabStrip radTabStrip)  
    {  
        ComparableTab[] tabs = new ComparableTab[radTabStrip.Items.Count];  
     
        for (int i = 0; i < tabs.Length; i++)  
        {  
            ComparableTab tab = new ComparableTab((TabItem)radTabStrip.Items[i]);  
            tabs[i] = tab;  
        }  
     
        radTabStrip.Items.Clear();  
     
        Array.Sort(tabs);  
     
        for (int i = 0; i < tabs.Length; i++)  
        {  
            radTabStrip.Items.Add(tabs[i].TabItem);  
        }  

    I hope this helps. If you need additional assistance, do not hesitate to contact me again.
     

    Sincerely yours,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Carisch
    Carisch avatar
    25 posts
    Member since:
    Aug 2007

    Posted 21 Apr 2008 Link to this post

    Thanks for your response.  Did you know if you clear the RadTabStrip, the objects within are never disposed which causes handles leaks? 

    Did you know when you add them back, they will lose some of their visual properties?

    I can find a work around to dispose the tabItems, and not lose valuable handles, but I'm can't figure out how to make the tabs retain this property:

    tab.TextOrientation = Orientation.Horizontal;

    You see, i have my tabs configurable.  They can be "flipped" from either top or left positions.  If the user currently has them on left position, and I excute the code below, the tabs will show up running down the left side, text going vertical.  This is not what I want.  Any suggestions.

    I'm really suprised there isn't a simpler solution such as VisibleIndex within the tab control, so developers won't have to remove then add back the tabs in a new order.

    thanks,
    Brian.

     

  5. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 23 Apr 2008 Link to this post

    Hello Brian,

    Thank you for contact me again.

    I have simplified my example by implementing the IComparer interface instead of ICompareble. You should not worry about the TabItem collection when you call the Clear() method, because they are reference types and there will not be a memory leak. Because of the same reason, you will not lose any settings, children controls, and event handlers for every single tab. I have prepared an example application that demonstrate this - you could find it attached. In addition, you could preview the code-block below as reference:
     
    public class TabsComparer : IComparer  
    {
        #region IComparer Members  
     
        public int Compare(object x, object y)  
        {  
            if ((x is RadItem) && (y is RadItem))  
            {  
                return ((RadItem)x).Text.CompareTo(((RadItem)y).Text);  
            }  
            else 
            {  
                throw new Exception("Provided objects is not a RadItem!");  
            }  
        }
        #endregion  
    }  
     
    private void SortTabs(RadTabStrip radTabStrip)  
    {  
        IComparer tabsComparer = new TabsComparer();  
        RadItem[] tabArr = radTabStrip.Items.ToArray();  
        Array.Sort(tabArr, tabsComparer);  
        radTabStrip.Items.Clear();  
        radTabStrip.Items.AddRange(tabArr);  

    I hope this will better suit your scenario. If you have other questions, do not hesitate to contact me again.
     

    Regards,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  6. Carisch
    Carisch avatar
    25 posts
    Member since:
    Aug 2007

    Posted 23 Apr 2008 Link to this post

    Thanks for the IComparer code.  I'm more interested in discussing the code:

        radTabStrip.Items.Clear();  
        radTabStrip.Items.AddRange(tabArr);  


    Have you tried this code?  You lose visual settings, and you DO have a handle leak issue.  I'm not sure about memory.

    Try creating a tabstrip and place 3 tabs on the designer.  make the tabs vertical, and place them on the left.

    this.radTabStrip1.TabsPosition = Telerik.WinControls.UI.TabPositions.Left;

    this.radTabStrip1.TextOrientation = System.Windows.Forms.Orientation.Vertical;



    in a button click

    RadItem test = radTabStrip1.Items[0];

    radTabStrip1.Items.Clear();

    radTabStrip1.Items.Add(test);


    run program, and observe the USER Objects on this process through your task manager.  Also note the position of the text.



    Brian.
  7. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 24 Apr 2008 Link to this post

    Hello Brian,

    In the old version Q3 2007 SP1, there is a issue related to the TextOrientation property of RadTabStrip and you are right about the wrong behavior. However, this has been fixed in our new version Q1 2008.  Please upgrade your project to Q1 2008 and download the example project in my previous post. It should work as in your scenario.
     
    I hope this was useful to you. Do not hesitate to contact me again if you have other questions.

    Sincerely yours,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top
UI for WinForms is Visual Studio 2017 Ready