如何对 ToolStripItemCollection 中的项目进行排序?
我通过以下方式将字符串(项目)动态添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
ToolStripItemCollection
没有“排序”功能,因此您必须监听更改并编写自己的排序方法:您必须使用自己的比较器(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:You have to use your own comparer (https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist.sort)
这篇文章被标记为 c#,所以我根据 SpeziFish 的答案对其进行了转换。谢谢!
This post was tagged as c# so I converted it based on SpeziFish's answer. Thanks!
如果我们需要对
ToolStripItemCollection
中的项目进行排序,我们可以使用以下内容:If we need to sort items in
ToolStripItemCollection
, we can use the following: