如何获取 .Net Forms 多选列表框中的最后一个选定项目?显然,如果我在列表框中选择一个项目,然后再选择 10 个,则所选项目是第一个。
我想获得我选择/取消选择的最后一个元素。
我会采用这种一般方法:
每次监听SelectedIndexChanged
事件并扫描集合。SelectedIndices
保留所有选定索引的单独列表,附加未在列表中的索引,删除已取消选择的索引。
单独的列表将按用户选择的时间顺序包含索引。最后一个元素始终是最近选择的索引。
// 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);
}
不确定我是否理解这个问题,但最后一个选定的项目将是 SelectedItems 数组中的最后一个,所以这样的事情应该可以工作:
ListItem i = list.SelectedItems[list.SelectedItems.Length-1];
在列表框的鼠标单击事件中使用以下代码:
private void ListBox1_MouseClick(object sender, MouseEventArgs e)
{
string s = ListBox1.Items[ListBox1.IndexFromPoint(e.Location)].ToString();
MessageBox.Show(s);
}
试试这个
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;
}
显然, LatestItemSelected是多余的,是为了强调您已经找到了您的项目。
使用一些反射来获取 FocusedIndex 的值,这是 ListBox 的内部属性,您可以获得最后一个关注的项目。
int lastSelectedIndex = (int)typeof(ListBox).GetProperty("FocusedIndex",BindingFlags.NonPublic|BindingFlags.Instance).GetValue(myListBox,null);
SelectedItemType mySelectedItem = myListBox.Items[lastSelectedIndex] as SelectedItemType;
使用 FocusManager.GetFocusedElement(listbox) 或 Keyboard.FocusedElement 返回您选择的最后一项。
我在 VBA 中遇到了完全相同的问题...想法是在隐藏标签中记录以前的选择...然后将列表框中的所有选定项目与记录的以前的选择进行比较...如果它不存在在标签中,这是最后一个选定的项目。我已经对其进行了测试,并且可以正常工作……您将不得不稍微调整一下代码,但这个想法对我有用。
Private Sub ListBox2_Change()
'Preview Doc
Previous_Selections = Me.Label2.Caption
'This logic figures out the last selected item if you already have existing selections
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
MyItem = ListBox2.List(i)
'Check if this is a new selection or and old one
If IsIn(MyItem, Previous_Selections) = False Then
'This is a new selection
'Record new selection for as 'previous' for next time
For n = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(n) Then Selections = Selections & ListBox2.List(n) & ", "
Next
'Preview last selected item
FullPath = Me.ListBox1.Value & "\" & MyItem
Me.Label2.Caption = Selections
Call Preview_Document(FullPath)
Exit Sub
End If
End If
Next i
End Sub
这是我在VB中完成的方式。
当您刷新列表框时,您必须重新调整数组的尺寸。
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