问题
In form1 I have two listboxs: listbox1, listbox2; loadbutton and savebutton
This code will write listbox1.selecteditem into a txt file and loadbutton will load the info.
But in listbox2 I want loadbutton to check if that item already exist in listbox2, if not write selected item from listbox1 and if that item already exist in listbox2 then do not save it (msg"this item is already exist in listbox2")
This is not working
Dim wri As New IO.StreamWriter("e:\test.txt", True)
If ListBox2.ToString.Contains(ListBox1.Items.Item) Then ' or ListBox1.SelectedItem ? ' not work
MsgBox("this item is already in listbox2")
Else
wri.WriteLine(ListBox1.SelectedItem, True)
End If
wri.Close()
回答1:
Change you code to the following:
If ListBox2.Items.Contains(ListBox1.Items.Item) Then ' or ListBox1.SelectedItem ? ' not work
MsgBox("this item is already in listbox2")
Else
wri.WriteLine(ListBox1.SelectedItem, True)
End If
wri.Close()
来源:https://stackoverflow.com/questions/9537952/check-if-an-item-is-already-exist-in-listbox1