如何对 ToolStripItemCollection 中的项目进行排序?

发布于 2024-10-19 03:38:03 字数 369 浏览 1 评论 0原文

我通过以下方式将字符串(项目)动态添加到 ToolStripItemCollection:

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

因此,这些项目不是一次性添加的,而是在整个程序会话期间根据外部触发器逐一添加。我想在每次添加新项目时对下拉列表进行排序。我有什么选择来实现这一目标?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白色秋天 2024-10-26 03:38:03

由于 ToolStripItemCollection 没有“排序”功能,因此您必须监听更改并编写自己的排序方法:

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

您必须使用自己的比较器(https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist .排序)

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)

澜川若宁 2024-10-26 03:38:03

这篇文章被标记为 c#,所以我根据 SpeziFish 的答案对其进行了转换。谢谢!

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);
    }
}

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);
    }
}
囚我心虐我身 2024-10-26 03:38:03

如果我们需要对 ToolStripItemCollection 中的项目进行排序,我们可以使用以下内容:

ItemCollection.OfType<ToolStripItem>().OrderBy(x => x.Text).ToArray(); 

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

ItemCollection.OfType<ToolStripItem>().OrderBy(x => x.Text).ToArray(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文