Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Apr 16, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 Long

Private 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 i
End 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.
Jeremy Adams
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date