End Sub Public Function ListIndexByItem(ByRef ctl As Control, _ ByVal strItem As String) As Integer
On Error GoTo HandleError
Dim o_strItem As String
If Not (ctl Is Nothing) Then Select Case TypeName(ctl) Case "ListBox", "ComboBox" With ctl o_strItem = .Text '保存原来的text .Text = strItem ListIndexByItem = .ListIndex .Text = o_strItem '恢复原来的text End With
Case Else ListIndexByItem = -1 End Select Else ListIndexByItem = -1 End If
HandleError: ListIndexByItem = -1
End Function
通过API可以检测某一选项的INDEX Public Const CB_FINDSTRINGEXACT = &H158index=SendMessage(Combo1.hwnd,CB_FINDSTRINGEXACT ,-1,ByVal strSerach) 'strSearch="女"
Public Const CB_FINDSTRING = &H14C '字头匹配,效果如"%女" Public Const CB_FINDSTRINGEXACT = &H158 '整字匹配,效果如"女"
End Sub Public Function ListIndexByItem(ByRef ctl As Control, _ ByVal strItem As String) As Integer '*********************************** '* '* 利用ListBox和ComboBox的.ListIndex特性, '* 不用API和循环判断,根据给定项目获取ListIndex '* '*********************************** On Error GoTo HandleError
Dim o_strItem As String
If Not (ctl Is Nothing) Then Select Case TypeName(ctl) Case "ListBox", "ComboBox" With ctl o_strItem = .Text '保存原来的text .Text = strItem ListIndexByItem = .ListIndex .Text = o_strItem '恢复原来的text End With
Case Else ListIndexByItem = -1 '没有找到 End Select Else ListIndexByItem = -1 '没有找到 End If Exit Function
HandleError: ListIndexByItem = -1 '没有找到
End Function Private Sub Form_Load()
With Combo1 .AddItem "男" .AddItem "女" .AddItem "(未知)" .ListIndex = 0 End With
if Combo1.Text="女" Then..End If
ComboBox1、ComboBox2,他们的List都添加有“男、女”,style都为dropdown list,当选中ComboBox1为“女”时,怎么让ComboBox2也选中“女”,不能用ComboBox2.ListIndex = ComboBox1.ListIndex;
MsgBox ListIndexByItem(Combo1, "女")
End Sub
Public Function ListIndexByItem(ByRef ctl As Control, _
ByVal strItem As String) As Integer
On Error GoTo HandleError
Dim o_strItem As String
If Not (ctl Is Nothing) Then
Select Case TypeName(ctl)
Case "ListBox", "ComboBox"
With ctl
o_strItem = .Text '保存原来的text
.Text = strItem
ListIndexByItem = .ListIndex
.Text = o_strItem '恢复原来的text
End With
Case Else
ListIndexByItem = -1
End Select
Else
ListIndexByItem = -1
End If
HandleError:
ListIndexByItem = -1
End Function
Public Const CB_FINDSTRINGEXACT = &H158index=SendMessage(Combo1.hwnd,CB_FINDSTRINGEXACT ,-1,ByVal strSerach) 'strSearch="女"
Public Const CB_FINDSTRINGEXACT = &H158 '整字匹配,效果如"女"
MsgBox ListIndexByItem(Combo1, "女")
MsgBox ListIndexByItem(Combo1, "哈哈")
End Sub
Public Function ListIndexByItem(ByRef ctl As Control, _
ByVal strItem As String) As Integer
'***********************************
'*
'* 利用ListBox和ComboBox的.ListIndex特性,
'* 不用API和循环判断,根据给定项目获取ListIndex
'*
'*********************************** On Error GoTo HandleError
Dim o_strItem As String
If Not (ctl Is Nothing) Then
Select Case TypeName(ctl)
Case "ListBox", "ComboBox"
With ctl
o_strItem = .Text '保存原来的text
.Text = strItem
ListIndexByItem = .ListIndex
.Text = o_strItem '恢复原来的text
End With
Case Else
ListIndexByItem = -1 '没有找到
End Select
Else
ListIndexByItem = -1 '没有找到
End If
Exit Function
HandleError:
ListIndexByItem = -1 '没有找到
End Function
Private Sub Form_Load()
With Combo1
.AddItem "男"
.AddItem "女"
.AddItem "(未知)"
.ListIndex = 0
End With
End Sub