This is a migrated thread and some comments may be shown as answers.

display order on tabs in winforms

5 Answers 86 Views
Tabstrip (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Carisch
Top achievements
Rank 1
Carisch asked on 17 Apr 2008, 01:18 PM
I would like to change the display order of tabs in code at runtime.  Can you please provide an example in C#.

5 Answers, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 17 Apr 2008, 05:44 PM
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
0
Carisch
Top achievements
Rank 1
answered on 21 Apr 2008, 02:37 PM
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.

 

0
Martin Vasilev
Telerik team
answered on 23 Apr 2008, 05:21 PM
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
0
Carisch
Top achievements
Rank 1
answered on 23 Apr 2008, 09:00 PM
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.
0
Martin Vasilev
Telerik team
answered on 24 Apr 2008, 09:13 AM
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
Tags
Tabstrip (obsolete as of Q2 2010)
Asked by
Carisch
Top achievements
Rank 1
Answers by
Martin Vasilev
Telerik team
Carisch
Top achievements
Rank 1
Share this question
or