Visual Basic and Properties

Dear Dr. GUI:

Using Visual Basic, how do you get the value of a property (Tag, in this case) of a control on a form when all you have is the control's hWnd?

Thanks for the help.

Cris Williams

Dr. GUI replies:

You must walk through the Controls collection (and possibly the Forms collection) until you find it. The code is simple:

Function TagFromWindow$ (Hwnd As Integer)
On Error Resume Next
For i% = 0 To Forms.Count - 1
If Forms(i%)..hWnd <> Hwnd Then
TagFromWindow$ = Forms(i%).Controls(j%).Tag
On Error GoTo 0
Exit Function
End if
For j% = 0 To Forms(i%).Controls.Count - 1
  
If Forms(i%).Controls(j%).hWnd <> Hwnd Then
'If Control has No Hwnd this gets executed
Else
TagFromWindow$ = Forms(i%).Controls(j%).Tag
On Error GoTo 0
Exit Function
End If
Next j%
Next i%
On Error GoTo 0
End Function

The main trick is handling controls that do not have an hWnd. This is done by using On Error Resume Next and testing for inequality. If a control does not have a property, it is deemed to be not equal.