How to have MyCustomControl Handle ParentForm.KeyDown event?  
Author Message
Mr.Analogy





PostPosted: Visual Basic Language, How to have MyCustomControl Handle ParentForm.KeyDown event? Top

 
I specifically want to do this:
 
 MyControl_KeyDown  (...) handles ContainingForm.KeyDown
 
So that, for example, if the user click "A" then MyControl will "know about it" even if MyControl doesn't have the focus.
 
 
 
 
TIA!
 
 



Visual Basic14  
 
 
Mr.Analogy





PostPosted: Visual Basic Language, How to have MyCustomControl Handle ParentForm.KeyDown event? Top

OK, solved my own problem:

AddHandler ParentForm.KeyDown, AddressOf KeyDownOnForm

Where : keydownonform is my custom Sub that handles the KeyDown event.

Curiously, I get an error when calling this in the NEW event of the control.



 
 
rkimble





PostPosted: Visual Basic Language, How to have MyCustomControl Handle ParentForm.KeyDown event? Top

The custom control does not have a parent set at the time of instantiation. You probalby want to handle the control's ParentChanged event and add the handler in there:

Private Sub UserControl1_ParentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ParentChanged

If Not Me.Parent Is Nothing Then

AddHandler ParentForm.KeyDown, AddressOf KeyDownOnForm

End If

End Sub

You'll have to ensure that a parent exists before adding the handler.

GL!