运行以下函数时,会发生运行时错误:
Public Sub SelectComboItemByValue(ByRef cboorlstCntrl As Object, ByRef lngItemValue As Long)
Dim cntrl As Control
Dim intcounter As Integer
cntrl = cboorlstCntrl
For intcounter = 0 To cntrl.list.items.count - 1
If cntrl.itemdata(intcounter) = lngItemValue Then
cntrl.selectedindex = intcounter
Exit For
End If
Next
cboorlstCntrl = cntrl
End Sub
答案 0 :(得分:0)
您需要将cntrl
声明为ComboBox
,并将cboorlstCntrl
参数投放到其中,如下所示:
If TypeOf(cboorlstCntrl) Is ComboBox Then
Dim cntrl As ComboBox = DirectCast(cboorlstCntrl, ComboBox)
Dim intcounter As Integer
cntrl = cboorlstCntrl
For intcounter = 0 To cntrl.Items.Count - 1
If cntrl.Items(intcounter) = lngItemValue Then
cntrl.SelectedIndex = intcounter
Exit For
End If
Next
cboorlstCntrl = cntrl
End If
通过这样做,cntrl
将拥有您期望的所有方法,例如.Items
属性。顺便说一句,.Items
属性将是您要用于获取控件中的项目的属性...组合框没有.List
属性。我已更新您的For
循环以反映此情况。