有一个String a,怎样从一个ListBox中去掉和这个String a 内容相同的项呢?

Nirvana0 2003-09-29 12:55:06
有一个String a,怎样从一个ListBox中去掉和这个String a 内容相同的项呢?
...全文
72 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
luzufu 2003-09-29
  • 打赏
  • 举报
回复
API函数SendMessage比较快,而且也好,能实现的!
LichKingSZ 2003-09-29
  • 打赏
  • 举报
回复
我支持 cuizm(射天狼) 的API方法,简单方便.用SendMessage估计也不会出现什么意外错误.
liul17 2003-09-29
  • 打赏
  • 举报
回复
清除 ListBox 及 ComboBox 中重复的项目

当我们要将一大堆资料加入 ListBox 或 ComboBox 时,为了不让 ListBox 或 ComboBox 中的项目重复,有些人会在将新项目加入 ListBox 或 ComboBox 时,就先作项目比对,资料没有重复时,才将资料加入 ListBox 或 ComboBox 中。

但是如果我们将资料统统加入 ListBox 或 ComboBox 之后,再来执行比对动作,不但程序容易维护,而且速度会加快一点点,以下的模组就是做项目比对,以清除 ListBox 或 ComboBox 中重复的项目。

模组中需要传入二个参数,说明如下:

1、控制项名称:可传入 ListBox 或 ComboBox 的名称。
2、是否分别大小写:True 表示要分别大小写,False 则不分大小写。

Sub RemoveDups(lst As Control, comptype As Boolean)
Dim lPos As Long '原始比对项目 index
Dim lCompPos As Long '待比对项目 index
Dim sComp As String '原始比对字串
Dim sComptype As Long '0(binary) / 1(textual) 比对

lPos = 0
If comptype Then sComptype = 0 Else sComptype = 1
Do While lPos < (lst.ListCount - 1)
sComp = lst.List(lPos)
lCompPos = lPos + 1
Do While lCompPos < lst.ListCount
If StrComp(sComp, lst.List(lCompPos), sComptype) = 0 Then
lst.RemoveItem lCompPos
lCompPos = lCompPos - 1
End If
lCompPos = lCompPos + 1
Loop
lPos = lPos + 1
Loop
End Sub

'在程序中使用方式如下:

'要分别大小写
Private Sub Command1_Click()
RemoveDups List1, True
RemoveDups Combo1, True
End Sub
'不分别大小写
Private Sub Command2_Click()
RemoveDups List1, False
RemoveDups Combo1, False
End Sub
射天狼 2003-09-29
  • 打赏
  • 举报
回复
大家都觉得循环快吗?晕,无论是TREEVIEW ,LISTVIEW ,COMBO控件,LISTBOX控件
我都用API方法,我感觉绝对比循环快~~
aha99 2003-09-29
  • 打赏
  • 举报
回复
建议用3楼的
program2100 2003-09-29
  • 打赏
  • 举报
回复
循环遍历
daviddivad 2003-09-29
  • 打赏
  • 举报
回复
楼上的写错了

If List1.ListCount > 0 Then
Dim i As Integer
For i = List1.ListCount To 0 Step -1
If List1.List(i) = Trim(Text1.Text) Then
List1.RemoveItem i
End If
Next i
End If
sunnyfire 2003-09-29
  • 打赏
  • 举报
回复
如果不用API
if list1.listcount>0 then
dim i as integer
For i =list.ListCount to 0 setp -1
If list.List(i) = Trim(text1.text) Then
list1.removeitem i
End If
Next i
end if
射天狼 2003-09-29
  • 打赏
  • 举报
回复
加一个LISTBOX和一个TEXTBOX控件,此例子的功能是删除LISTBOX控件中跟TEXT中输入一样的内容!
射天狼 2003-09-29
  • 打赏
  • 举报
回复
Option Explicit

Private Const LB_FINDSTRINGEXACT = &H1A2
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Command1_Click()
Dim ls_FindString As String, ll_Rtn As Long

ls_FindString = Trim(Text1.Text) & Chr(0)
ll_Rtn = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal ls_FindString)
If ll_Rtn <> -1 Then List1.RemoveItem ll_Rtn
End Sub


7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧