2014-11-22 118 views
0

我遇到了一個我不明白的問題,並且挫折阻止了我的過程。搜索一個列表框,當搜索結果找到時結果將不會突出顯示

我有一個文本文件被讀取並加載到列表框中。我有一個搜索工作在以前的嘗試,但功能不是很正確,我有一個單獨的列表框中的結果,但無法找出刪除過程,因爲它不會更新刪除後的原始列表框。所以我選擇保持簡單,所以我想,只是想用一個列表框。

textchanged txtSearch代碼在下面,它不會突出顯示單個搜索項目。我試過lstMain.SelectedItem = True,lstMain.SelectedItem = item我猜這是因爲lstMain.SelectedItem需要一個Integer值,我使用字符串來選擇每行內的一定數量的數據該文本文件與列和行類似。

For Each item As String In lstMain.SelectedItems 
    If item.ToUpper.Substring(0, 24).Contains(strSearchField.ToUpper) Then 
     lstMain.SelectedItem 
      End If 
      Next 

回答

1

你的邏輯只有在最後一項是匹配時纔有效。

For Each item As String In lstMain.SelectedItems 
    If item.ToUpper.Substring(0, 24).Contains(strSearchField.ToUpper) Then 
    lstMain.SelectedItem = item 
    Exit For 'so the found item stays selected 
    End If 
Next 

For i As Integer = 0 To lstMain.Items.Count - 1 
    If lstMain.Items(i).ToUpper.Substring(0, 24).Contains(strSearchField.ToUpper) Then 
    lstMain.SelectedIndex = i 
    Exit For 'so the found item stays selected 
    End If 
Next 
+0

只有最後選擇的項目不是我想要的。我曾經認爲if語句會經歷lstMain.Items,如果它匹配了strSearchField,它會用lstMain.SelectedItem突出顯示每個匹配。退出爲確實刪除了原來的錯誤。 – VanCoon 2014-11-23 20:21:18

0

我認爲你需要強調的listbox搜索詞,不是嗎?如果是這樣,下面的代碼將幫助你:

Dim searchWord As String = "apple" 
If ListBox1.Items.Contains(searchWord) Then 
    ListBox1.SelectedItem = searchWord 
End If 
+0

我曾嘗試過,但是當我通過使用If ListBox1.Substring(10,20).Items獲得更具體的內容時,vb.net表示substring不是system.windows.forms.listbox的成員。這背後的原因是即使列表框中列出的項目確實有大寫和小寫字母,也要消除區分大小寫的搜索。 – VanCoon 2014-11-24 10:31:36