在 form1 中我有两个列表框:listbox1、listbox2;加载按钮和保存按钮
此代码会将 listbox1.selecteditem 写入 txt 文件,然后 loadbutton 将加载信息。
但在 listbox2 中我希望 loadbutton 检查该项目是否已存在于 listbox2,如果没有从 listbox1 中写入所选项目,并且该项目已存在于 listbox2 中,则不保存它(msg“该项目已存在于 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()
最佳答案
将代码更改为以下内容:
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()
关于vb.net - 检查 listbox1 中是否已存在某个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9537952/