Attribute VB_Name = "modObjectFunc" Option Explicit '+P '=============================================================== ' Control (Objects) Enhancement Functions '--------------------------------------------------------------- ' Purpose : Provides extended functions for controls ' : - Searches in List & Combo Boxes ' : - Identify objects by type,name,etc. using string descriptors ' (without having to use conditional statement blocks) ' Notes : '--------------------------------------------------------------- 'Revision History '--------------------------------------------------------------- ' 20040526 : Initial Version '=============================================================== Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As _ Integer, ByVal lParam As Any) As Long 'AlwaysOnTheTop - Move a form on top of all other windows Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, _ ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, _ ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private Const SWP_NOSIZE = &H1 Private Const SWP_NOMOVE = &H2 Private Const SWP_SHOWWINDOW = &H40 Private Const HWND_NOTOPMOST = -2 Private Const HWND_TOPMOST = -1 'constants for searching the ComboBox Private Const CB_FINDSTRINGEXACT = &H158 Private Const CB_FINDSTRING = &H14C 'constants for searching the ListBox Private Const LB_FINDSTRINGEXACT = &H1A2 Private Const LB_FINDSTRING = &H18F ' Set a form always on the top. ' ' the form can be specified as a Form or object ' or through its hWnd property ' If OnTop=False the always on the top mode is de-activated. Public Sub SetAlwaysOnTopMode(hWndOrForm As Variant, Optional ByVal OnTop As Boolean = _ True) Dim hWnd As Long ' get the hWnd of the form to be move on top If VarType(hWndOrForm) = vbLong Then hWnd = hWndOrForm Else hWnd = hWndOrForm.hWnd End If SetWindowPos hWnd, IIf(OnTop, HWND_TOPMOST, HWND_NOTOPMOST), 0, 0, 0, 0, _ SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW End Sub 'function to find an item in a ComboBox or ListBox 'Parameters: ' listOBJ - the ComboBox or ListBox control. ' SearchKey - item that you would like to search for. Can be any string - case doesn't matter when searching ' Optional FindExactMatch - Default is True. Pass False to find a partial match 'Return: ' Returns the index of the found match. If the match is not found, -1 is returned 'Usage: ' listOBJ.ListIndex = GetListObjIndex(listOBJ, "Test Item") ' listOBJ.ListIndex = GetListObjIndex(listOBJ, "Test Item", False) Public Function GetListObjIndex(listOBJ As Object, SearchKey As String, Optional FindExactMatch As Boolean = True) As Long GetListObjIndex = -1 If (TypeOf listOBJ Is ComboBox) And FindExactMatch Then GetListObjIndex = SendMessage(listOBJ.hWnd, CB_FINDSTRINGEXACT, -1, ByVal SearchKey) ElseIf (TypeOf listOBJ Is ComboBox) Then GetListObjIndex = SendMessage(listOBJ.hWnd, CB_FINDSTRING, -1, ByVal SearchKey) ElseIf (TypeOf listOBJ Is ListBox) And FindExactMatch Then GetListObjIndex = SendMessage(listOBJ.hWnd, LB_FINDSTRINGEXACT, -1, ByVal SearchKey) ElseIf (TypeOf listOBJ Is ListBox) Then GetListObjIndex = SendMessage(listOBJ.hWnd, LB_FINDSTRING, -1, ByVal SearchKey) End If End Function 'Public Function GetListObjIndexEx(listOBJ As ComboBox, SearchKey As String, Optional FindExactMatch As Boolean = True) As Long Public Function GetListObjIndexEx(listOBJ As Object, SearchKey As String, Optional FindExactMatch As Boolean = True) As Long Dim strListItem As String Dim Index As Integer Dim MatchFound As Boolean Dim ExactMatchFound As Boolean GetListObjIndexEx = -1 'default if not found MatchFound = False If (FindExactMatch) Then ExactMatchFound = False Else ExactMatchFound = True End If For Index = 0 To listOBJ.ListCount - 1 strListItem = listOBJ.List(Index) If (InStr(1, strListItem, SearchKey, vbTextCompare)) Then MatchFound = True If (strListItem = SearchKey) Then ExactMatchFound = True If (MatchFound And ExactMatchFound) Then GetListObjIndexEx = Index Exit For End If Next End Function 'can use typeof 'tests name of object, not case sensitive Public Function isObjByName(ByRef objObj As Object, strObjName As String) As Boolean Dim strTemp As String Dim boolName As Boolean On Error GoTo errObjByName isObjByName = False boolName = Not CBool(StrComp(objObj.Name, strObjName, vbTextCompare)) isObjByName = boolName Exit Function errObjByName: End Function 'tests type of object, not case sensitive Public Function isObjByType(ByRef objObj As Object, strObjType As String) As Boolean Dim strTemp As String Dim boolType As Boolean On Error GoTo errObjByType isObjByType = False boolType = Not CBool(StrComp(TypeName(objObj), strObjType, vbTextCompare)) isObjByType = boolType Exit Function errObjByType: End Function 'tests name and type of object, not case sensitive Public Function isObjByNameType(ByRef objObj As Object, strObjName As String, strObjType As String) As Boolean On Error GoTo errObjByNameType isObjByNameType = False isObjByNameType = isObjByName(objObj, strObjName) And isObjByType(objObj, strObjType) Exit Function errObjByNameType: End Function 'tests the object by index Public Function isObjByIndex(ByRef objObj As Object, lngIndex) As Boolean On Error GoTo errObjByIndex isObjByIndex = IIf(lngIndex = objObj.Index, True, False) Exit Function errObjByIndex: End Function Public Sub EnableControlGroup(objControlGroup As Object, EnableControl As Boolean) Dim ctrlControl As Control For Each ctrlControl In objControlGroup ctrlControl.Enabled = EnableControl Next End Sub