首页 > 解决方案 > VB6比较list1和list2并从list2中删除不需要的项目

问题描述

我正在尝试比较 vb6 中的 2 个列表框,list2 项目应该匹配 list1 中的项目,然后从 list2 中删除不匹配的字符串。

清单 1 项:

ls-05
ls-06
ls-12
mg_01.rom
mg_02.rom
mg_05.rom
mg_06.rom
mg_m07.rom
mg_m08.rom
mg_m09.rom
mg_m10.rom
mg_m11.rom
mg_m12.rom
mg_m13.rom
mg_m14.rom

清单 2 项:

ls-05
ls-05.12e
ls-06
ls-06.10e
ls-11
ls-11.2l
ls-12
ls-12.7l
mg_01.rom
mg_02.rom
mg_05.rom
mg_06.rom
mg_m07.rom
mg_m07.rom2
mg_m08.rom
mg_m08.rom3
mg_m09.rom
mg_m09.rom2
mg_m10.rom
mg_m10.rom3
mg_m11.rom
mg_m11.rom0
mg_m12.rom
mg_m12.rom1
mg_m13.rom
mg_m13.rom0
mg_m14.rom
mg_m14.rom1

按钮代码:

For ii = List1.ListCount - 1 To 0 Step -1
    For i = List1.ListCount - 1 To 0 Step -1
        If List1.List(i) = List2.List(ii) Then Exit For ' no need to keep looping, its a match. i will be > -1
    Next
    If i = -1 Then ' all items in list1 were searched, but no matches found, so remove it
        List2.RemoveItem ii
    End If
Next

所以我追求的最终结果是 list2 应该有相同的项目删除其他不匹配的垃圾字符串。

标签: vb6

解决方案


使用字符串和 InStrB() 函数:

dim lstitm as string, str2 as string, count as integer

count = list2.listcount

for i = 0 to count - 1
  str2 = str2 & list2.list(i) & ";"
next i

list2.clear

count = list1.listcount

for i = 0 to count -1
 lstitm = list1.list(i)
 if instrb(1,str2,lstitm) <> 0 then list2.additem lstitm
next i

推荐阅读