How to get the last selected item in multiselect ListBox?

泪湿孤枕 提交于 2019-11-29 15:34:42

I would take this general approach:

Listen for the SelectedIndexChanged event and scan through the SelectedIndices collection every time.

Keep a separate list of all selected indices, appending ones that have not been in the list, removing those that have been de-selected.

The separate list will contain the indexes in the chronological order they were selected by the user. The last element always is the most recently 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);
}

Not sure 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];
Aman Ahmed

In the mouse click event of listbox use following code:

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

    MessageBox.Show(s);
}

Try this

 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 LatestItemSelected is redundant and is there to emphasize that you have found your item.

using a bit of reflection to get the value of FocusedIndex which is an internal property of ListBox you can get the last focused on item.

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

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

This is way I've done it in VB.

When you refresh the list box you have to re-dimension 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!