2015-10-13 128 views
0

我有一個兩個列表框列表框1和列表框2。我想要獲取ListBox1中的所有重複項目,我將在TextBox中搜索並將其放入ListBox2中,並且當我搜索的所有重複項目都在ListBox2中時,它的自動計數請幫助我。從一個列表框選擇項目到另一個列表框

例如,在ListBox1中項

DOG 
DOG 
DOG 
CAT 
CAT 

當我在文本框鍵入DOG在ListBox1中所有的DOG將被複制到ListBox2。我該怎麼做?

我想這

Dim check As Boolean 
For Each item In ListBox1.Items 
    check = ListBox1.FindStringExact(item) 
    ListBox2.Items.Add(item) 
Next 

我也嘗試這一點,但它錯了伯爵的,我會尋找確切的字前行。 例如 DOG DOG DOG CAT CAT 我會搜索在文本框CAT在listbox2輸出爲3 這裏是我的代碼:

昏暗的支票作爲字符串

check = ListBox1.FindStringExact(TextBox1.Text) 
    ListBox2.Items.Add(check) 

回答

1
listBox2.Items.AddRange(listBox1.Items.Cast(Of ListItem)().Where(Function(x) x.Text = TextBox1.Text).ToArray(Of ListItem)()) 
+0

先生它不是在我工作:( – NewProg

+0

你得到任何錯誤? –

+0

是的,先生,我有在列表項一個錯誤,那麼我應該用它做什麼? – NewProg

0

在文本框中輸入值後,請在按鈕上單擊下面的代碼。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim searchFor As String = TextBox1.Text 
    For Each item In ListBox1.Items 
     If item = searchFor Then 
      ListBox2.Items.Add(item) 
     End If 
    Next 

    Do While ListBox1.Items.Contains(searchFor) 
     ListBox1.Items.Remove(searchFor) 
    Loop 
End Sub 
+0

謝謝你!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!你的我的救星! !!!!!! – NewProg

+0

但先生有可能?複製不刪除listbox1中的項目?到listbox2? – NewProg

+0

只需刪除Do While代碼即可解決您的問題,請將其標記爲答案 – Ian

相關問題