How to get the last selected item in a Multiselect ListBox?

How to get the last selected item in .Net Forms multiselect ListBox? Apparently if I select an item in the list and then select another 10, the selected item comes first.

I would like to get the last item that I have selected / deselected.

+1


source to share


7 replies


I would take this general approach:

Listen to the event SelectedIndexChanged

and view it through the collection SelectedIndices

every time.

Keep a separate list of all selected indexes, adding those that were not in the list, deleting those that were canceled.



A separate list will contain the indexes in chronological order selected by the user. The last item is always the last selected index.

// for the sake of the example, I defined a single List<int>
List<int> listBox1_selection = new List<int>();

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    TrackSelectionChange((ListBox)sender, listBox1_selection);
}

private void TrackSelectionChange(ListBox lb, List<int> selection)
{
    ListBox.SelectedIndexCollection sic = lb.SelectedIndices;
    foreach (int index in sic)
        if (!selection.Contains(index)) selection.Add(index);

    foreach (int index in new List<int>(selection))
        if (!sic.Contains(index)) selection.Remove(index);
}

      

+7


source


Not sure if I understand the question, but the last selected item will be the last in the SelectedItems array, so something like this should work:



ListItem i = list.SelectedItems[list.SelectedItems.Length-1];

      

+5


source


In the click event from the list, use the following code:

private void ListBox1_MouseClick(object sender, MouseEventArgs e)
{
    string s = ListBox1.Items[ListBox1.IndexFromPoint(e.Location)].ToString();

    MessageBox.Show(s);
}

      

+4


source


try it

 private void listBox1_MouseUp(object sender, MouseEventArgs e)
    {
        int jj = listBox1.IndexFromPoint(e.X, e.Y);
        object Test = listBox1.Items[jj];
        object LatestItemSelected;
        if(listBox1.SelectedItems.Contains(Test))
            LatestItemSelected = Test;
    }

      

Obviously LastItemSelected is redundant and should emphasize that you've found your item.

0


source


using the reflection bit to get the FocusedIndex which is an internal property of the ListBox, you can get the last one focused on the element.

int lastSelectedIndex = (int)typeof(ListBox).GetProperty("FocusedIndex",BindingFlags.NonPublic|BindingFlags.Instance).GetValue(myListBox,null);
SelectedItemType mySelectedItem = myListBox.Items[lastSelectedIndex] as SelectedItemType;

      

0


source


Use FocusManager.GetFocusedElement (listbox) or Keyboard.FocusedElement to return the last element you selected.

0


source


This is how I did it in VB.

When you update the list box, you need to resize the array.

  Dim SelectedAry(-1) As Integer

  Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim LastOne As Integer = -1
        ' First time there no elements in the preserved array
        If SelectedAry.Length = 0 Then
              If ListBox1.SelectedIndex <> -1 Then
                    LastOne = 0
              End If
        Else
              'If the SelectedIndices array is larger than the preserved SelectedAry - means that another one had been selected
              If ListBox1.SelectedIndices.Count >= SelectedAry.Length Then
                    For i = 0 To ListBox1.SelectedIndices.Count - 1
                          'Go through both arrays comparing the values until there is a mismatch
                          'This means that the value in the  SelectesIndices is the last one to be added
                          If ListBox1.SelectedIndices(i) <> SelectedAry(i) Then
                                LastOne = i
                                Exit For
                          End If
                    Next
              End If
        End If
        ' Copy the Listbox selectedindices array into the SelectedAry which is preserved for the next selected index change
        ReDim SelectedAry(ListBox1.SelectedIndices.Count)
        For i = 0 To ListBox1.SelectedIndices.Count - 1
              SelectedAry(i) = ListBox1.SelectedIndices(i)
        Next
        ' Display the last one added
        If LastOne >= 0 Then
              Dim FileName As String = txtFolder.Text & "\" & ListBox1.Items(ListBox1.SelectedIndices(LastOne)).ToString
              Display_File(FileName)
        Else
        End If
  End Sub

      

-1


source







All Articles