Nasty, nasty, nasty!!!
Turns out that "For Each itm As Control In Me.Controls" was not sufficient as child controls contained in controls were not getting referenced...
Resolved the problem, but surely there must be a better way of handling this problem?
Code:
Custom control:
Public Event ConfirmSelection()
Private Sub txtText_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtText.LostFocus
RaiseEvent ConfirmSelection()
End Sub
The EventHandler for the panel:
Private Sub ConfirmSelection() Handles InputBox.ConfirmSelection
If Not (HaveFocus(Me)) Then
Me.Visible = False
End If
End Sub
Check to see if Focus has been passed into a control on the panel:
Private Function HaveFocus(ByVal ctl As Control) As Boolean
Dim result As Boolean = False
If ctl.Focused Then
result = True
Else
If ctl.HasChildren Then
For Each itm As Control In ctl.Controls
If HaveFocus(itm) Then
result = True
Exit For
End If
Next
End If
End If
Return result
End Function
In the Panel.Load() event, handle all the LostFocus() events for EVERY control on the panel!!!:
Private Sub ctlDropDown_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddLostFocusHandler(Me)
End Sub
Private Sub AddLostFocusHandler(ByVal ctl As Control)
For Each itm As Control In ctl.Controls
AddHandler itm.LostFocus, AddressOf ConfirmSelection
If itm.HasChildren Then AddLostFocusHandler(itm)
Next
End Sub
Bookmarks