vb在list1中有n个数,输入一个数如何判断这个数在list1中是否已经存在了?
3个回答
展开全部
简单的方法:
Private Sub Command1_Click()
Dim x As String, i As Integer, s As Boolean
x = InputBox("请输入一个数:")
s = True
If List1.ListCount = 0 Then s = False
For i = 0 To List1.ListCount - 1
If List1.List(i) = x Then
s = False
Exit For
End If
Next
If s = False Then
MsgBox (x & "已存在!")
else
MsgBox (x & "不存在!")
end if
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 100
List1.AddItem (i)
Next
End Sub
点击command1开始判断,inputbox输入你要判断的数,form_load事件对list1进行初始化,方便你进行调试
Private Sub Command1_Click()
Dim x As String, i As Integer, s As Boolean
x = InputBox("请输入一个数:")
s = True
If List1.ListCount = 0 Then s = False
For i = 0 To List1.ListCount - 1
If List1.List(i) = x Then
s = False
Exit For
End If
Next
If s = False Then
MsgBox (x & "已存在!")
else
MsgBox (x & "不存在!")
end if
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 100
List1.AddItem (i)
Next
End Sub
点击command1开始判断,inputbox输入你要判断的数,form_load事件对list1进行初始化,方便你进行调试
展开全部
我只会用笨办法:
for i=0 to list1.listcount-1
temp=temp & list1.list(i) & chr(32)
next
if instr(temp ,cstr(n) & chr(32))=0 then
list1.additem n
endif
哦。刚刚想起来,也可以用API来实现。
Private Declare Function SendMessage _
Lib "user32.dll" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Const LB_FINDSTRINGEXACT As Long = &H1A2
下面为添加事件里的代码:
Dim result As Long
result = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, 0, ByVal Text1.Text)
If result > 0 Then
MsgBox "该值已经存在!"
Else
List1.AddItem Text1.Text
End If
for i=0 to list1.listcount-1
temp=temp & list1.list(i) & chr(32)
next
if instr(temp ,cstr(n) & chr(32))=0 then
list1.additem n
endif
哦。刚刚想起来,也可以用API来实现。
Private Declare Function SendMessage _
Lib "user32.dll" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Const LB_FINDSTRINGEXACT As Long = &H1A2
下面为添加事件里的代码:
Dim result As Long
result = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, 0, ByVal Text1.Text)
If result > 0 Then
MsgBox "该值已经存在!"
Else
List1.AddItem Text1.Text
End If
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
for i:=0 to list1.listcount-1
if n=list1.list(i) then msgbox("已存在")
next i
if n=list1.list(i) then msgbox("已存在")
next i
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询