如何将列表分组并存储索引?

发布于 2024-09-08 11:21:41 字数 581 浏览 8 评论 0原文

我有一个列表框,其中包含 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 技术交流群。

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

发布评论

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

评论(2

旧话新听 2024-09-15 11:21:41

如果您指的是 winforms,ListView 内置了 .Groups(每个 ListViewGroup),每个 ListViewItem 都有一个.Group。这将使您在代码中所需要的内容变得简单,并且对用户来说视觉上直观。

请注意,仅当 .ViewView.Details.ShowGroupstrue 且可视化时才会显示组样式已启用(Application.EnableVisualStyles(),通常在 Main() 中)。

If you mean winforms, ListView has .Groups (each of ListViewGroup) built in, with each ListViewItem 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 is View.Details, .ShowGroups is true, and visual styling is enabled (Application.EnableVisualStyles(), usually in Main()).

流年里的时光 2024-09-15 11:21:41

我自己设法解决了这个问题,方法如下:

        // Create string to save last 'used' group in.
        string lastGroup = string.Empty;

        // Create counter to check what index we are at in the ListBox.
        int i = 0;

        // Create a dictionary to store <string Item, string Group>.
        Dictionary<string, string> dictionary = new Dictionary<string, string>(); 

        // Loop every item (as string) in the ListBox.
        foreach (string o in lbxMain.Items)
        {
            // If the item is a group:
            if (o.StartsWith("Group:"))
                // Put the name of the item into the lastGroup variable so we know where to put the items in.
                lastGroup = lbxMain.Items[i].ToString();

            // If the item is an item:
            if (o.StartsWith("Item:"))
                // Put the item into a dictionary with the lastGroup variable saying what group it's part of.
                dictionary .Add(o + " " + i, lastGroup);

            // Increase i so we keep an eye on the indices.
            i++;
        }

如果您只想要代码:

        string lastGroup = string.Empty;
        int i = 0;
        Dictionary<string, string> dictionary = new Dictionary<string, string>(); 

        foreach (string o in lbxMain.Items)
        {
            if (o.StartsWith("Group:"))
                lastGroup = lbxMain.Items[i].ToString();
            if (o.StartsWith("Item:"))
                dictionary.Add(o + " " + i, lastGroup);
            i++;
        }

I've managed to solve it myself and this is how:

        // Create string to save last 'used' group in.
        string lastGroup = string.Empty;

        // Create counter to check what index we are at in the ListBox.
        int i = 0;

        // Create a dictionary to store <string Item, string Group>.
        Dictionary<string, string> dictionary = new Dictionary<string, string>(); 

        // Loop every item (as string) in the ListBox.
        foreach (string o in lbxMain.Items)
        {
            // If the item is a group:
            if (o.StartsWith("Group:"))
                // Put the name of the item into the lastGroup variable so we know where to put the items in.
                lastGroup = lbxMain.Items[i].ToString();

            // If the item is an item:
            if (o.StartsWith("Item:"))
                // Put the item into a dictionary with the lastGroup variable saying what group it's part of.
                dictionary .Add(o + " " + i, lastGroup);

            // Increase i so we keep an eye on the indices.
            i++;
        }

And if you just want the code:

        string lastGroup = string.Empty;
        int i = 0;
        Dictionary<string, string> dictionary = new Dictionary<string, string>(); 

        foreach (string o in lbxMain.Items)
        {
            if (o.StartsWith("Group:"))
                lastGroup = lbxMain.Items[i].ToString();
            if (o.StartsWith("Item:"))
                dictionary.Add(o + " " + i, lastGroup);
            i++;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文