如何将列表分组并存储索引?
我有一个列表框,其中包含 6 个值。使用按钮可以将新项目插入列表框中。我可以使用我添加的其他按钮上下移动所有这些项目。为了便于理解,我们将新创建的项目(我想将其用作组/分隔符)称为“组”。我想要完成的是存储组之间的项目。例如通过 SortedDictionary
。示例(请注意,括号内的数字是索引):
Group 1 [0]
Item 1 [1]
Item 2 [2]
Item 3 [3]
Group 2 [4]
Item 4 [5]
Item 5 [6]
Group 3 [7]
Item 6 [8]
字典可能如下所示:
1, "group 1"
2, "group 1"
3, "group 1"
5, "group 2"
6, "group 2"
8, "group 3"
其中第一个数字是列表框中的项目索引,第二个数字(字符串)是它所属的组。
所以我的问题是:如何循环列表框,以便我可以检查哪些项目属于哪个组?如果有更简单的方法(使用与列表框不同的控件),我也很乐意尝试。
I have a list box which holds, say, 6 values. Using buttons it's possible to insert new items into the list box. I can move all of these items up and down using other buttons I've added. For the purpose of understanding we'll call the newly created items (which I want to act as groups/dividers) "groups". What I want to accomplish is to have the items between the groups stored. For example through a SortedDictionary<int itemIndex, string group>
. An example (note that the bracketed number is the index):
Group 1 [0]
Item 1 [1]
Item 2 [2]
Item 3 [3]
Group 2 [4]
Item 4 [5]
Item 5 [6]
Group 3 [7]
Item 6 [8]
The dictionary could look like this:
1, "group 1"
2, "group 1"
3, "group 1"
5, "group 2"
6, "group 2"
8, "group 3"
Where the first number is an item index in the list box and the second (string) is the group it falls under.
So my question is: how do I loop the list box in such a way that I can check which items fall under what group? If there's an easier way of doing it (using a different control than a list box) I'm happy to try that too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您指的是 winforms,
ListView
内置了.Groups
(每个ListViewGroup
),每个ListViewItem
都有一个.Group
。这将使您在代码中所需要的内容变得简单,并且对用户来说视觉上直观。请注意,仅当
.View
为View.Details
、.ShowGroups
为true
且可视化时才会显示组样式已启用(Application.EnableVisualStyles()
,通常在Main()
中)。If you mean winforms,
ListView
has.Groups
(each ofListViewGroup
) built in, with eachListViewItem
having a.Group
. This should make what you need easy in the code, and visually intuitive for the user.Note that groups are only displayed when the
.View
isView.Details
,.ShowGroups
istrue
, and visual styling is enabled (Application.EnableVisualStyles()
, usually inMain()
).我自己设法解决了这个问题,方法如下:
如果您只想要代码:
I've managed to solve it myself and this is how:
And if you just want the code: