VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



ListIndex (ListBox OR ComboBox) from ItemData

by Dipen Anovadia (19 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Sun 18th December 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

ListIndex (ListBox OR ComboBox) from ItemData

API Declarations


Public Enum IFID_RETURN
IFID_NOERROR = 0
IFID_ERR_OBJ = 1
IFID_ERR_INPUT = 2
IFID_ERR_UNKNOWN = 3
IFID_ERR_EMPTYLIST = 4
End Enum

'IndexFromItemData
'Get the Index for List Object (ListBox OR ComboBox) from ItemData
'
'IN:
' lo - List Object (ListBox OR ComboBox)
' ItemData - ItemData to find
'(OPTIONAL)
' Start - Starting point (0 For first, by default)
' Finish - Finishing point (-1 For all, by default)
'
'OUT:
' lReturnError - Returns the error code
'
'RETURN:
' -1 - Failure, found No Index
' (Long) - Found Index
'
'NOTE: ItemData must be unique otherwise first item first change...


Rate ListIndex (ListBox OR ComboBox) from ItemData




On Error GoTo errIFID

    Dim i As Integer
    Dim lCount As Long
    Dim iStep As Integer

    IndexFromItemData = -1

    If Not (TypeOf lo Is ListBox Or _
            TypeOf lo Is ComboBox) Then
        lReturnError = IFID_ERR_OBJ
        Exit Function
    End If

    lCount = lo.ListCount

    If lCount = 0 Then
        lReturnError = IFID_ERR_EMPTYLIST
        Exit Function
    End If

    iStep = 1

    If Start >= lCount Then
        If Finish < Start And Finish <= lCount Then
            iStep = -1
        Else
            lReturnError = IFID_ERR_INPUT
            Exit Function
        End If
    Else
        If Finish = -1 Then Finish = lCount
        'If Finish < Start < lCount Then iStep = -1
        If Finish < Start Then iStep = -1 Else iStep = 1
    End If

    For i = Start To Finish Step iStep
        If lo.ItemData(i) = ItemData Then
            IndexFromItemData = i
            Exit Function
        End If
    Next i

    Exit Function
errIFID:
    lReturnError = IFID_ERR_UNKNOWN
    IndexFromItemData = -1

    'ShowError
End Function
'Please tell me about Bugs, comments, feedbacks, etc, if any...
'Example:
'Sub SelectItemData3()
'    'List1/Combo1
'    Dim lIndex As Long
'    lIndex = ItemFromIndexData(List1, 3)
'    If lIndex > -1 then List1.ListIndex = lIndex
'End Sub

Download this snippet    Add to My Saved Code

ListIndex (ListBox OR ComboBox) from ItemData Comments

No comments have been posted about ListIndex (ListBox OR ComboBox) from ItemData. Why not be the first to post a comment about ListIndex (ListBox OR ComboBox) from ItemData.

Post your comment

Subject:
Message:
0/1000 characters