How to sort items in ToolStripItemCollection?
Asked Answered
P

3

6

I add strings (items) dynamically to a ToolStripItemCollection by:

Dim onClickHandler As System.EventHandler = New System.EventHandler(AddressOf Symbol_Click)
Dim item As New ToolStripMenuItem(newSymbol, Nothing, onClickHandler)
SomeToolStripMenuItem.DropDownItems.Add(item)

So the items are not added in one go, but one-by-one based on external triggers throughout the program session. I would like to sort the drop-down-list every time I add a new item. What are my options to achieve that?

Probably answered 24/2, 2011 at 8:37 Comment(0)
X
7

Since ToolStripItemCollection has no "Sort"-Function, you have to listen on changes and write your own sort-method:

Private Sub ResortToolStripItemCollection(coll As ToolStripItemCollection)
    Dim oAList As New System.Collections.ArrayList(coll)
    oAList.Sort(new ToolStripItemComparer())
    coll.Clear()

    For Each oItem As ToolStripItem In oAList
        coll.Add(oItem)
    Next
End Sub

Private Class ToolStripItemComparer Implements System.Collections.IComparer
    Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim oItem1 As ToolStripItem = DirectCast(x, ToolStripItem)
        Dim oItem2 As ToolStripItem = DirectCast(y, ToolStripItem)
        Return String.Compare(oItem1.Text,oItem2.Text,True)
    End Function
End Class

You have to use your own comparer (https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist.sort)

Xylophone answered 24/2, 2011 at 15:3 Comment(3)
It looked nice, but i get the exception: <code>failed to compare two elements in the array - at least one object must implement icomparable.</code> Does this refer to the object class itself; ToolStripMenuItem? I will see if I understand the custom comparer..Probably
Oh sorry, you MUST use an own comparer, because there is none for ToolStripMenuItem, so the array doesn't know how to sort. I updated the code above.Xylophone
Cool! I would have struggled with that one.. Cheers!Probably
I
4

This post was tagged as c# so I converted it based on SpeziFish's answer. Thanks!

private void ResortToolStripItemCollection(ToolStripItemCollection coll)
    {
        System.Collections.ArrayList oAList = new System.Collections.ArrayList(coll);
        oAList.Sort(new ToolStripItemComparer());
        coll.Clear();

        foreach (ToolStripItem oItem in oAList)
        {
            coll.Add(oItem);
        }
    }

public class ToolStripItemComparer : System.Collections.IComparer
{
    public int Compare(object x, object y)
    {
        ToolStripItem oItem1 = (ToolStripItem)x;
        ToolStripItem oItem2 = (ToolStripItem)y;
        return string.Compare(oItem1.Text, oItem2.Text, true);
    }
}
Indaba answered 24/4, 2015 at 15:54 Comment(0)
F
0

If we need to sort items in ToolStripItemCollection, we can use the following:

ItemCollection.OfType<ToolStripItem>().OrderBy(x => x.Text).ToArray(); 
Foreworn answered 1/7, 2015 at 12:48 Comment(1)
I assume you ment ItemCollection = ... ? this will not work though since the DropDownItems property is read-only.Seppuku

© 2022 - 2024 — McMap. All rights reserved.