Attribute VB_Name = "modControls" Option Explicit '============================================================================================================= ' ' modControls Module ' ------------------ ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .net ) ' ' Created On : August 8, 2002 ' Last Update : August 8, 2002 ' ' VB Versions : 5.0 / 6.0 ' ' Requires : Nothing ' ' Description : This module is meant to make it easy to find and use controls on parent objects (i.e. - Form, ' MDIForm, UserControl, PropertyPage or any other parent object which supports the "Controls" ' collection property and "hWnd" property) based on their names, handles, and/or parent handles. ' ' NOTE : This doesn't work for the following controls because their underlying "Control" ' object interfaces don't support the handle (hWnd) property: ' Data, Timer, Image, OLE, Line, Shape, Label ' ' Example Use : ' ' Option Explicit ' Private Sub Form_Load() ' On Error Resume Next ' Dim objControl As Control ' Dim udtControls() As ControlInfo ' Dim lngControlsCount As Long ' Dim lngCounter As Long ' ' Get the information about the controls on this form ' If GetControls(Me, udtControls, lngControlsCount) = False Then ' MsgBox "The specified parent object is invalid or doesn't support the 'Controls' collection property", vbOKOnly + vbExclamation, " " ' Else ' ' Display the information about the controls in this form within the IMMEDIATE WINDOW ' If lngControlsCount > 0 Then ' For lngCounter = 0 To lngControlsCount - 1 ' With udtControls(lngCounter) ' Debug.Print "Name = " & .c_Name ' Debug.Print "Index = " & .c_Index ' Debug.Print "Clase = " & .c_Class ' Debug.Print "Type = " & .c_Type ' Debug.Print "hWnd = " & .c_hWnd ' Debug.Print "hWnd_Sub = " & .c_hWnd_Sub ' Debug.Print "hParent = " & .c_hParent ' Debug.Print "----------------------" ' End With ' Next ' End If ' ' Set the BackColor property of one of the controls ' udtControls(1).c_Control.BackColor = vbRed ' ' Clean up ' Erase udtControls ' lngControlsCount = 0 ' End If ' End Sub ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= ' Enumeration - Standard Visual Basic Control types Public Enum ControlTypes ct_Unknown = 0 ct_Picturebox = 1 ct_Label = 2 ct_TextBox ct_Frame ct_CommandButton ct_CheckBox ct_OptionButton ct_ComboBox ct_ListBox ct_HScrollBar ct_VScrollBar ct_Timer ct_DriveListBox ct_DirListBox ct_FileListbox ct_Shape ct_Line ct_Image ct_Data ct_OLE ctx_Calendar8 'CalendarWndClass - Microsoft Calendar Control 8.0 [MSCAL.OCX] ctx_Chart 'MSChartWndClass - Microsoft Chart Control [MSCHART.OCX] ctx_Chart6 'MSChart20WndClass - Microsoft Chart Control 6.0 [MSCHRT20.OCX] ctx_DataBoundGrid5 'ApexGrid.19 - Microsoft Data Bound Grid Control 5.0 [DBGRID32.OCX] ctx_DataBoundList6_ListBox 'DBListWndClass - Microsoft Data Bound List Controls 6.0 [DBLIST32.OCX] (ListBox) ctx_DataBoundList6_ComboBox 'DBComboWndClass - Microsoft Data Bound List Controls 6.0 [DBLIST32.OCX] (ComboBox) ctx_DataGrid6 'DataGridWndClass - Microsoft DataGrid Control 6.0 [MSDATGRD.OCX] ctx_DataList6_ListBox 'DataListWndClass - Microsoft DataList Controls 6.0 [MSDATLST.OCX] (Listbox) ctx_DataList6_ComboBox 'DataComboWndClass - Microsoft DataList Controls 6.0 [MSDATLST.OCX] (ComboBox) ctx_FlexGrid6 'MSFlexGridWndClass - Microsoft FlexGrid Control 6.0 [MSFLXGRD.OCX] ctx_InternetControl 'Shell Embedding - Microsoft Internet Controls [SHDOCVW.DLL] ctx_MaskedEdit6 'MSMaskWndClass - Microsoft Masked Edit Control 6.0 [MSMASK32.OCX] ctx_Multimedia6 'mciWndClass - Microsoft Multimedia Control 6.0 [MCI32.OCX] ctx_RichTextBox6 'RichTextWndClass - Microsoft Rich TextBox Control 6.0 [RICHTX32.OCX] ctx_Common5_TabStrip 'TabStripWndClass - Microsoft Windows Common Controls 5.0 [COMCTL32.OCX] (TabStrip) ctx_Common5_Toolbar 'ToolbarWndClass - Microsoft Windows Common Controls 5.0 [COMCTL32.OCX] (Toolbar) ctx_Common5_StatusBar 'StatusBarWndClass - Microsoft Windows Common Controls 5.0 [COMCTL32.OCX] (StatusBar) ctx_Common5_ProgressBar 'ProgressBarWndClass - Microsoft Windows Common Controls 5.0 [COMCTL32.OCX] (ProgressBar) ctx_Common5_TreeView 'TreeViewWndClass - Microsoft Windows Common Controls 5.0 [COMCTL32.OCX] (TreeView) ctx_Common5_Listbiew 'ListViewWndClass - Microsoft Windows Common Controls 5.0 [COMCTL32.OCX] (ListView) ctx_Common5_Slider 'SliderWndClass - Microsoft Windows Common Controls 5.0 [COMCTL32.OCX] (Slider) ctx_Common52_Animation 'AnimationWndClass - Microsoft Windows Common Controls-2 5.0 [COMCT232.OCX] (Animation) ctx_Common52_UpDown 'UpDownWndClass - Microsoft Windows Common Controls-2 5.0 [COMCT232.OCX] (UpDown) ctx_Common6_ImageCombo 'ImageCombo20WndClass - Microsoft Windows Common Controls 6.0 [MSCOMCTL.OCX] (ImageCombo) ctx_Common6_Slider 'Slider20WndClass - Microsoft Windows Common Controls 6.0 [MSCOMCTL.OCX] (Slider) ctx_Common6_ListView 'ListView20WndClass - Microsoft Windows Common Controls 6.0 [MSCOMCTL.OCX] (ListView) ctx_Common6_TreeView 'TreeView20WndClass - Microsoft Windows Common Controls 6.0 [MSCOMCTL.OCX] (TreeView) ctx_Common6_ProgressBar 'ProgressBar20WndClass - Microsoft Windows Common Controls 6.0 [MSCOMCTL.OCX] (ProgressBar) ctx_Common6_StatusBar 'StatusBar20WndClass - Microsoft Windows Common Controls 6.0 [MSCOMCTL.OCX] (StatusBar) ctx_Common6_ToolBar 'Toolbar20WndClass - Microsoft Windows Common Controls 6.0 [MSCOMCTL.OCX] (ToolBar) ctx_Common6_TabStrip 'TabStrip20WndClass - Microsoft Windows Common Controls 6.0 [MSCOMCTL.OCX] (TabStrip) ctx_Common62_Animation 'Animation20WndClass - Microsoft Windows Common Controls-2 6.0 [MSCOMCT2.OCX] (Animation) ctx_Common62_UpDown 'UpDown20WndClass - Microsoft Windows Common Controls-2 6.0 [MSCOMCT2.OCX] (UpDown) ctx_Common62_MonthView 'MonthView20WndClass - Microsoft Windows Common Controls-2 6.0 [MSCOMCT2.OCX] (MonthView) ctx_Common62_DTPicker 'DTPicker20WndClass - Microsoft Windows Common Controls-2 6.0 [MSCOMCT2.OCX] (DTPicker) ctx_Common62_FlatScrollBar 'FlatSB20WndClass - Microsoft Windows Common Controls-2 6.0 [MSCOMCT2.OCX] (FlatScrollBar) ctx_Common63_CoolBar 'ThunderRT6UserControlDC - Microsoft Windows Common Controls-3 6.0 [COMCT332.OCX] (CoolBar) End Enum ' Type Definition - Information about the control found Public Type ControlInfo c_Name As String ' The name assigned to the VB Control c_Index As Integer ' The index number of the control (if the control is an element of a Control Array) c_hWnd As Long ' The handle of the VB Control c_hWnd_Sub As Long ' If the control is a ComboBox, this specifies the handle of the TextBox (Edit) control contained within it c_hParent As Long ' The handle of of the VB Control's parent c_Class As String ' The name of VB Control's class (native VB controls start with "Thunder") c_Type As ControlTypes ' The type of the control c_Control As Control ' A reference to the Control object itself (which can be used to get other properties like "Height", etc) End Type ' This is used to pass information back and forth between the CALLBACK function that enumerates through ' the specified parent object (Form, UserControl, etc) and the "GetControls" function. Private TempControlInfo() As ControlInfo Private TempControlInfoCount As Long ' Win32 API Declarations Private Declare Function EnumChildWindows Lib "USER32.DLL" (ByVal hwndParent As Long, ByVal lpEnumCallback As Long, ByVal lParam As Long) As Long Private Declare Function GetClassName Lib "USER32.DLL" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal ClassName As String, ByVal ClassNameSize As Long) As Long Private Declare Function GetParent Lib "USER32.DLL" (ByVal hWnd As Long) As Long '============================================================================================================= ' GetControl ' ' Purpose : ' This function takes the specified control name and looks for it within the specified parent object and ' returns a reference to it if found. ' ' Param Use ' ------------------------------------ ' strControlName The name of the control you wish to locate ' objParentObject This is a reference to the parent object to search for controls on. This can be a ' Form, MDIForm, UserControl, PropertyPage, or any other object that supports the ' "Controls" collection property and "hWnd" property. ' Return_Control If the specified control was found, this returns a reference to it ' ' Return ' ------ ' Returns TRUE if the specified control was found on the specified parent object ' Returns False if the specified control was NOT found on the specified parent object ' '============================================================================================================= Public Function GetControl(ByVal strControlName As String, ByRef objParentObject As Object, ByRef Return_Control As Control) As Boolean Dim objControl As Control ' Clear return values Set Return_Control = Nothing ' Loop through the specified object and search for the specified control For Each objControl In objParentObject.Controls If UCase(Trim(strControlName)) = UCase(Trim(objControl.Name)) Then Set Return_Control = objControl GetControl = True Exit For End If Next End Function '============================================================================================================= ' GetControls ' ' Purpose : ' This function loops through all the controls found within the specified parent object (Form, UserControl, etc) ' and gets all the information on each control it can. ' ' Param Use ' ------------------------------------ ' objParentObject This is a reference to the parent object to search for controls on. This can be a ' Form, MDIForm, UserControl, PropertyPage, or any other object that supports the ' "Controls" collection property and "hWnd" property. ' Return_Controls This is an array of information returned for the controls on the specified parent object. ' Return_ControlCount Optional. This is the number of controls found on the specified parent object. ' ' Return ' ------ ' Returns TRUE if executed successfully ' Returns FALSE if an error occured while executing ' '============================================================================================================= Public Function GetControls(ByRef objParentObject As Object, ByRef Return_Controls() As ControlInfo, Optional ByRef Return_ControlCount As Long) As Boolean On Error Resume Next Dim objControl As Control Dim lngCounter As Long ' Clear the variables we'll be working with Erase TempControlInfo TempControlInfoCount = 0 Erase Return_Controls Return_ControlCount = 0 ' Loop through the controls on the form specified and get the information we can For Each objControl In objParentObject.Controls If objControl Is Nothing Then Err.Clear: Exit Function ReDim Preserve TempControlInfo(0 To TempControlInfoCount) As ControlInfo Set TempControlInfo(TempControlInfoCount).c_Control = objControl TempControlInfo(TempControlInfoCount).c_Name = objControl.Name TempControlInfo(TempControlInfoCount).c_Index = objControl.Index TempControlInfo(TempControlInfoCount).c_hWnd = objControl.hWnd TempControlInfoCount = TempControlInfoCount + 1 Next ' Have windows go throug each control and get info If EnumChildWindows(objParentObject.hWnd, AddressOf EnumChildProc, 0) = 1 Then ' Copy the information retrieved from the temporary variables into the return variables Return_ControlCount = TempControlInfoCount ReDim Return_Controls(TempControlInfoCount - 1) As ControlInfo For lngCounter = 0 To TempControlInfoCount - 1 Set Return_Controls(lngCounter).c_Control = TempControlInfo(lngCounter).c_Control With TempControlInfo(lngCounter) Return_Controls(lngCounter).c_Name = .c_Name Return_Controls(lngCounter).c_Index = .c_Index Return_Controls(lngCounter).c_Class = .c_Class Return_Controls(lngCounter).c_Type = .c_Type Return_Controls(lngCounter).c_hWnd = .c_hWnd Return_Controls(lngCounter).c_hWnd_Sub = .c_hWnd_Sub Return_Controls(lngCounter).c_hParent = .c_hParent End With Next End If ' Clean up the temporary variables used Erase TempControlInfo TempControlInfoCount = 0 ' Clear any error information that may have occured been captured Err.Clear ' Function executed successfully GetControls = True End Function 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ' This is the CALLBACK function used to itterate through all the controls in the specified paranet object (Form, UserControl, etc) Private Function EnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Long 'BOOL On Error Resume Next Dim strClassname As String Dim hParent As Long Dim lngCounter As Long ' Setup the buffer for the class name strClassname = String(260, Chr(0)) ' Get the class name If GetClassName(hWnd, strClassname, Len(strClassname)) <> 0 Then strClassname = Left(strClassname, InStr(strClassname, Chr(0)) - 1) Else strClassname = "" End If ' Loop through the controls and assign the classname to them If TempControlInfoCount > 0 Then ' If the current control is of the class "Edit", it's the TextBox contained within a ComboBox, ' so loop through the controls and assign this hWnd to the "Sub" handle of that ComboBox If UCase(Trim(strClassname)) = "EDIT" Then hParent = GetParent(hWnd) If hParent <> 0 Then For lngCounter = 0 To TempControlInfoCount - 1 If TempControlInfo(lngCounter).c_hWnd = hParent Then TempControlInfo(lngCounter).c_hWnd_Sub = hWnd Exit For End If Next End If ' If the current control is NOT of the class "Edit", match it to a previously found control and get it's info Else For lngCounter = 0 To TempControlInfoCount - 1 If TempControlInfo(lngCounter).c_hWnd = hWnd Then With TempControlInfo(lngCounter) .c_Class = strClassname .c_hParent = GetParent(hWnd) .c_Type = GetControlType(strClassname) End With Exit For End If Next End If End If ' Clear any error information that may have occured been captured Err.Clear ' Continue looping through the controls EnumChildProc = 1 End Function ' This function takes the class name and looks for known Visual Basic control classes. If the ' specified class name matches a known Visual Basic control class name, then it returns the ' type of control it is. Otherwise, it returns UNKNOWN. Private Function GetControlType(ByVal strClassname As String) As ControlTypes Select Case Trim(strClassname) Case "ThunderData": GetControlType = ct_Data Case "ThunderFileListBox": GetControlType = ct_FileListbox Case "ThunderDirListBox": GetControlType = ct_DirListBox Case "ThunderDriveListBox": GetControlType = ct_DriveListBox Case "ThunderTimer": GetControlType = ct_Timer Case "ThunderVScrollBar": GetControlType = ct_VScrollBar Case "ThunderHScrollBar": GetControlType = ct_HScrollBar Case "ThunderListBox": GetControlType = ct_ListBox Case "ThunderComboBox": GetControlType = ct_ComboBox Case "ThunderOptionButton": GetControlType = ct_OptionButton Case "ThunderCheckBox": GetControlType = ct_CheckBox Case "ThunderCommandButton": GetControlType = ct_CommandButton Case "ThunderFrame": GetControlType = ct_Frame Case "ThunderTextBox": GetControlType = ct_TextBox Case "ThunderPictureBox", "ThunderPictureBoxDC": GetControlType = ct_Picturebox Case "CalendarWndClass": GetControlType = ctx_Calendar8 Case "MSChartWndClass": GetControlType = ctx_Chart Case "MSChart20WndClass": GetControlType = ctx_Chart6 Case "ApexGrid.19": GetControlType = ctx_DataBoundGrid5 Case "DBListWndClass": GetControlType = ctx_DataBoundList6_ListBox Case "DBComboWndClass": GetControlType = ctx_DataBoundList6_ComboBox Case "DataGridWndClass": GetControlType = ctx_DataGrid6 Case "DataListWndClass": GetControlType = ctx_DataList6_ListBox Case "DataComboWndClass": GetControlType = ctx_DataList6_ComboBox Case "MSFlexGridWndClass": GetControlType = ctx_FlexGrid6 Case "Shell Embedding": GetControlType = ctx_InternetControl Case "MSMaskWndClass": GetControlType = ctx_MaskedEdit6 Case "mciWndClass": GetControlType = ctx_Multimedia6 Case "RichTextWndClass": GetControlType = ctx_RichTextBox6 Case "TabStripWndClass": GetControlType = ctx_Common5_TabStrip Case "ToolbarWndClass": GetControlType = ctx_Common5_Toolbar Case "StatusBarWndClass": GetControlType = ctx_Common5_StatusBar Case "ProgressBarWndClass": GetControlType = ctx_Common5_ProgressBar Case "TreeViewWndClass": GetControlType = ctx_Common5_TreeView Case "ListViewWndClass": GetControlType = ctx_Common5_Listbiew Case "SliderWndClass": GetControlType = ctx_Common5_Slider Case "AnimationWndClass": GetControlType = ctx_Common52_Animation Case "UpDownWndClass": GetControlType = ctx_Common52_UpDown Case "ImageCombo20WndClass": GetControlType = ctx_Common6_ImageCombo Case "Slider20WndClass": GetControlType = ctx_Common6_Slider Case "ListView20WndClass": GetControlType = ctx_Common6_ListView Case "TreeView20WndClass": GetControlType = ctx_Common6_TreeView Case "ProgressBar20WndClass": GetControlType = ctx_Common6_ProgressBar Case "StatusBar20WndClass": GetControlType = ctx_Common6_StatusBar Case "Toolbar20WndClass": GetControlType = ctx_Common6_ToolBar Case "TabStrip20WndClass": GetControlType = ctx_Common6_TabStrip Case "Animation20WndClass": GetControlType = ctx_Common62_Animation Case "UpDown20WndClass": GetControlType = ctx_Common62_UpDown Case "MonthView20WndClass": GetControlType = ctx_Common62_MonthView Case "DTPicker20WndClass": GetControlType = ctx_Common62_DTPicker Case "FlatSB20WndClass": GetControlType = ctx_Common62_FlatScrollBar Case "ThunderRT6UserControlDC": GetControlType = ctx_Common63_CoolBar Case Else: GetControlType = ct_Unknown End Select End Function