How to get the last selected item in a Multiselect ListBox?
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);
}
source to share
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.
source to share
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;
source to share
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
source to share