简体   繁体   English

如何使用Winforms C#获取用户在列表框中选择项目的顺序

[英]how do I get the order in which user selected Items in Listbox using Winforms c#

I am collecting all the items that a user has selected from a listbox and entering the values within a textbox in my application. 我正在收集用户从列表框中选择的所有项目,并在我的应用程序的文本框中输入值。 However, the items I receive from myListBox.SelectedItems are always sorted whereas I need to retain the order in which the user had selected those items. 但是,我从myListBox.SelectedItems收到的项目始终进行排序,而我需要保留用户选择这些项目的顺序。

I am using Winforms. 我正在使用Winforms。 I know that the listbox in WPF returns what I want but I am not using WPF just simple winform and C#. 我知道WPF中的列表框会返回我想要的内容,但我不是在使用WPF而是简单的winform和C#。

This is not proper UI. 这是不正确的UI。 If the order matters to you then it is also important to the user. 如果订单对您很重要,那么这对用户也很重要。 Who has no way to tell in what order he selected the items, other than by memorizing it. 除了记住之外,谁没有办法告诉他选择物品的顺序。 A simple interruption like a phone call is enough to get lost. 像电话这样的简单中断就足以丢失。

Instead, use two listboxes. 而是使用两个列表框。 Choices on the left, selected items on the right. 左边的选项,右边的选定项。 Provide buttons to let the user move them from one to the other and move an item up/down in the right one. 提供按钮以使用户将它们从一个移到另一个,并在右侧的一个中上下移动一个项目。 Now the order is obvious and modifiable. 现在顺序是显而易见的并且可以修改。 And your problem is solved as well. 您的问题也得到了解决。

A random image I found with Googles image search (ignore styling choices): 我在Google图片搜索中找到的随机图片(忽略样式选择):

在此处输入图片说明

You can add an event handler to SelectedIndexChanged to keep track of which items were selected and in what order. 您可以将事件处理程序添加到SelectedIndexChanged中,以跟踪选择了哪些项目以及选择的顺序。 For example: 例如:

namespace SandboxTest
{
    public partial class ListBox : Form
    {
        int[] SelectionOrder = new int[50];
        int ClickHistory = 0;

        public ListBox()
        {
            InitializeComponent();
        }

        private void CatchListSelection(object sender, EventArgs e)
        {
            if (ClickHistory == 42)
            {
                ClickHistory = 0;
            }
            SelectionOrder[ClickHistory] = listBox1.SelectedIndex;
            ClickHistory++;
        }
    }
}

The variable SelectionOrder then contains a list of the indexes that were selected. 然后,变量SelectionOrder包含所选索引的列表。 It looks like this in debug after clicking the a few different items: 单击一些不同的项目后,在调试中看起来像这样:

调试SelectionOrder数组的显示

Just don't forget to add "CatchListSelection" to the SelectedIndexChanged event of your listbox. 只是不要忘记将“ CatchListSelection”添加到列表框的SelectedIndexChanged事件。

You can try this code to record the order exactly and easily : 您可以尝试使用以下代码来准确轻松地 记录订单:

List<int> selectedIndices = new List<int>();
//SelectedIndexChanged event handler for your listBox1
private void listBox1_SelectedIndexChanged(object sender, EventArgs e){
   if (listBox1.SelectedIndex > -1){                
      selectedIndices.AddRange(listBox1.SelectedIndices.OfType<int>()
                                       .Except(selectedIndices));                                                               
      selectedIndices.RemoveRange(0, selectedIndices.Count - listBox1.SelectedItems.Count); 
   }
}
//Now all the SelectedIndices (in order) are saved in selectedIndices;
//Here is the code to get the SelectedItems in order from it easily
var selectedItems = selectedIndices.Select(i => listBox1.Items[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM