2008-11-20 74 views

回答

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); 
} 
+0

非常感謝,你們都有幫助 – Germstorm 2008-11-21 07:50:32

5

不知道我理解的問題,但最後選擇的項目將是最後的SelectedItems陣列中,所以這樣的事情應該工作:

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

返回列表中當前選定的最後一項,與用戶選擇的最後一項不同。例如,如果用戶選擇「項目3」,「項目1」,則您的解決方案將始終返回「項目3」。 – 2016-04-05 20:00:52

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

在下面的代碼列表框使用的鼠標點擊事件:

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

    MessageBox.Show(s); 
} 
-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 
+0

這個問題要求.net解決方案不是vb解決方案。 – 2016-08-23 20:34:21

0

使用位反射來獲得FocusedIndex的值是列表框的內部屬性,你可以得到的最後一個專注於項目。

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

使用FocusManager.GetFocusedElement(列表框)或Keyboard.FocusedElement返回你選擇的最後一個項目。

相關問題