vb.net - 检查 listbox1 中是否已存在某个项目

标签 vb.net listbox vb.net-2010

在 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/

相关文章:

excel - 将工作表添加到 Excel 工作簿

vb.net - ASP.NET "The request was aborted: Could not create SSL/TLS secure channel"也与配置的 servicepointmanager 一起出现

vb.net - Winforms:获取发布版本号?

c# - 使用c#在wpf的列表框中选择多个项目

vb.net - 将字符串从两个文本框转换为 hh :mm vb. NET

.net - 查找并替换字符串中的特定字符

.net - 创建包含多个音符(频率)的声音

javascript - 如何从列表框中选择多个值

wpf - 如果选择了该项目,则更改 ListBoxItem 中的 DataTemplate 样式

.net - VB.NET to English - 这行代码应该做什么?