Check if an item is already exist in listbox1

好久不见. 提交于 2020-01-04 07:53:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!