如何获取多选ListBox中的最后一个选定项?

时间:2008-11-20 15:01:23

标签: .net winforms listbox

如何获取.Net Forms多选ListBox中的最后一个选定项?显然,如果我在列表框中选择一个项目,然后选择另一个项目,那么所选项目就是第一个项目。

我想获取我选择/取消选择的最后一个元素。

8 个答案:

答案 0 :(得分:7)

我会采用这种一般方法:

收听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);
}

答案 1 :(得分:5)

我不确定我是否理解这个问题,但最后选择的项目将是SelectedItems数组中的最后一项,所以这样的事情应该有效:

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

答案 2 :(得分:4)

在列表框的鼠标单击事件中,使用以下代码:

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

    MessageBox.Show(s);
}

答案 3 :(得分:0)

试试这个

 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 是多余的,并且强调您找到了您的项目。

答案 4 :(得分:0)

使用一点反射来获取FocusedIndex的值,这是ListBox的内部属性,你可以得到最后一个关注项目。

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

答案 5 :(得分:0)

使用FocusManager.GetFocusedElement(listbox)或Keyboard.FocusedElement返回您选择的最后一项。

答案 6 :(得分:0)

我在 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

答案 7 :(得分:-1)

这是我在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