简体   繁体   English

如何在列表框中删除重复项VB.net

[英]How to remove duplicates in a listbox VB.net

I am trying to remove duplicates in a ListBox which is populated by a query pull. 我正在尝试删除由查询请求填充的ListBox中的重复项。 I use this code to prevent adding duplicates in VB 6.0 but does not work when converted over to VB.net. 我使用此代码来防止在VB 6.0中添加重复项,但是在转换为VB.net时不起作用。 Is there a substitute method to prevent or remove duplicates. 是否有替代方法来防止或删除重复项。

colSchema = dr("Col_Schema").ToString
If Not lstSchema.Items.ToString.Contains(colSchema) Then
     lstSchema.Items.Add(New ListItem(colSchema))
End If

This code 这段代码

lstSchema.Items.ToString

is converting Items to a string. 正在将Items转换为字符串。 Items is most likely the type ListBox.ObjectCollection (if this is WinForms) or a similar collection type for other UI frameworks. 项目很可能是ListBox.ObjectCollection类型(如果是WinForms)或其他UI框架的类似集合类型。 Calling ToString on such classes will end up calling Object.ToString, which just returns the name of the class. 在此类上调用ToString最终将调用Object.ToString,后者仅返回类的名称。

Instead, try 相反,尝试

lstSchema.Items.Contains(colSchema)

If that does not work for some reason, please update your question explaining exactly what you were trying to solve by calling ToString. 如果由于某些原因不能解决问题,请更新您的问题,以确切地解释您要通过调用ToString解决的问题。

try 尝试

colSchema = dr("Col_Schema").ToString
dim exists as boolean = false
for i as integer = 0 to lstSchema.items.count - 1
if lstSchema.items.item(i) = colSchema then
exists = true
end if
next
if exists = false then
lstSchema.Items.Add(New ListItem(colSchema))
end if

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM