【问题标题】:See the order of multiselection in ListBox C# WinForms查看 ListBox C# WinForms 中的多选顺序
【发布时间】:2016-03-08 09:48:04
【问题描述】:

谁能告诉我如何在 C# 的列表框中查看选定的整数顺序? 例如,如果我在列表框中有这个元素:

项目 1 项目 2 项目 3 项目 4 项目 5

我按此顺序选择 Item4、Item2 和 Item5,我需要一种方法来查找这些项目是如何按上述顺序选择的。

谢谢!

【问题讨论】:

  • 什么框架(WPF、WinForms)?虽然我怀疑它们中的任何一个都提供了开箱即用的功能。
  • WindowsForms 框架
  • @Stefan 然后编辑您的问题以包含适当的标签(winforms)。

标签: c# winforms multi-select listboxitems


【解决方案1】:

可能会获取所选项目的索引并将其添加到数组中吗?

【讨论】:

  • 我不认为我可以按照我选择项目的顺序获取索引
  • @ȘtefanBlaga 您应该对Cell(s)Selected 事件做出反应,并按照它们被选中的顺序存储当前选中的单元格。
  • @ Eugene Podskal 是的,我发现了。感谢您的反馈
【解决方案2】:

稍微扩展一下鲁道夫的想法,使用List<T>

List<int> selected = new List<int>();

您可以跟踪到目前为止选择的项目:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // add new selection:
    foreach (int index in listBox1.SelectedIndices) 
            if (!selected.Contains(index)) selected.Add(index);
    // remove unselected items:
    for (int i = selected.Count - 1; i >= 0; i-- ) 
        if (!listBox1.SelectedIndices.Contains(selected[i])) selected.Remove(i);
}

为了测试你可以写:

for (int i = 0; i < selected.Count; i++) 
   Console.WriteLine("Item # " selected[i] + ": " + listBox1.Items[selected[i]]);

请注意,当您使用扩展的多选选项时,您将获得通常的、有点奇怪的 Windows 选择顺序..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多