gmb Mc
gmb Mc

Reputation: 23

Check if an item is already exist in listbox1

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()

Upvotes: 2

Views: 10163

Answers (1)

jzworkman
jzworkman

Reputation: 2703

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()

Upvotes: 2

Related Questions