devxlogo

Retrieving a Control From the Controls Collection With an hWnd

Retrieving a Control From the Controls Collection With an hWnd

The GetDlgCtrlID API, when passed a valid hWnd, returns a value that directly corresponds to the Index property of the Controls collection:

 Private Declare Function GetDlgCtrlID Lib "user32" _	(ByVal hWnd As Long) As LongPrivate Sub Form_Load()	Dim i As Long	On Error Resume Next	For i = 0 To Controls.Count - 1		Debug.Print Controls(i).Name,		Debug.Print Controls(GetDlgCtrlID(Controls(i).hWnd) _			- 1).Name	Next iEnd Sub

This loop, located in the Form_Load event of a form with a number of controls on it, loops through all the controls and prints the name of each windowed control twice, demonstrating that it has correctly located the control without looping through the control collection.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist