简体   繁体   中英

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.

I am using Winforms. I know that the listbox in WPF returns what I want but I am not using WPF just simple winform and C#.

This is not proper 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):

在此处输入图片说明

You can add an event handler to SelectedIndexChanged to keep track of which items were selected and in what order. 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. 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.

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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