将vb 6代码转换为vb.net时出错

时间:2015-12-18 05:14:25

标签: vb.net

运行以下函数时,会发生运行时错误:

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

1 个答案:

答案 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循环以反映此情况。