本文介绍了检查listbox1中是否已经存在一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在form1中,我有两个列表框:listbox1,listbox2;加载按钮和保存按钮
In form1 I have two listboxs: listbox1, listbox2; loadbutton and savebutton
此代码会将listbox1.selecteditem写入txt文件,并且loadbutton将加载信息.
This code will write listbox1.selecteditem into a txt file and loadbutton will load the info.
但是在listbox2中,我希望loadbutton检查该项目是否已经存在listbox2,如果未从listbox1中写入所选项目,并且该项目已存在于listbox2中,则不要保存该项目(msg该项目已存在于listbox2中")
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")
这不起作用
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()
推荐答案
将代码更改为以下内容:
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()
这篇关于检查listbox1中是否已经存在一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!