Having my Event and Eating it?

P

Phill. W

How do I propagate an Event that I've just handled?

I have a UserControl containing, among other things, a TextBox.

The UserControl handles KeyDown/KeyPress Events for both itself
and the TextBox, as in

Private Sub TextField_KeyDown( _
ByVal sender As Object _
, ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles MyBase.KeyDown, txtField.KeyDown

My problem is that I'd /also/ like the Outside World to be able to
receive these Events as well. Should it be as simple as calling

MyBase.OnKeyDown()

within the above Event Handler?
Or is that going to give me a nasty loop, with the UserControl
handling its own Event, over and over again?

TIA,
Phill W.
 
K

Ken Tucker [MVP]

Hi,

Here is some code from an user control that has a textbox. It has
the user controls textchanged, and validating events fire when the textbox's
events fire. Also makes the user controls text property change the
textboxes text. Hope this helps.

Public Shadows Event TextChanged(ByVal sender As Object, ByVal e As
EventArgs)

Public Shadows Event Validating(ByVal sender As Object, ByRef e As
System.ComponentModel.CancelEventArgs)

Public Shadows Property Text() As String

Get

Return TextBox1.Text

End Get

Set(ByVal Value As String)

TextBox1.Text = Value

End Set

End Property

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

RaiseEvent TextChanged(Me, e)

End Sub

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating

RaiseEvent Validating(Me, e)

End Sub



Ken

---------------------

How do I propagate an Event that I've just handled?

I have a UserControl containing, among other things, a TextBox.

The UserControl handles KeyDown/KeyPress Events for both itself
and the TextBox, as in

Private Sub TextField_KeyDown( _
ByVal sender As Object _
, ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles MyBase.KeyDown, txtField.KeyDown

My problem is that I'd /also/ like the Outside World to be able to
receive these Events as well. Should it be as simple as calling

MyBase.OnKeyDown()

within the above Event Handler?
Or is that going to give me a nasty loop, with the UserControl
handling its own Event, over and over again?

TIA,
Phill W.
 
J

Jay B. Harlow [MVP - Outlook]

Phill,
Do you care who the sender is in the KeyDown event?

I would override the MyBase.OnKeyDown() to handle the user control's KeyDown
event, being certain to call MyBase.OnKeyDown so others see the event.

Then in the txtField.KeyDown event handler I would call MyClass.OnKeyDown.
This handler would only handle the TextBox's event.

Something like:

Protected Overrides Sub OnKeyDown(ByVal e As
System.Windows.Forms.KeyEventArgs)
' do your thing here
MyBase.OnKeyDown(e)
' or/also do your thing here
End Sub

Private Sub TextField_KeyDown( _
ByVal sender As Object _
, ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles txtField.KeyDown
MyClass.OnKeyDown(e)
End Sub

If you cared who the sender is I would have the above two methods call an
overloaded OnKeyDown method, which then calls MyBase.OnKeyDown.

Protected Overloads Sub OnKeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs)
' do your thing here
MyBase.OnKeyDown(e)
' or/also do your thing here
End Sub

Protected Overrides Sub OnKeyDown(ByVal e As
System.Windows.Forms.KeyEventArgs)
MyClass.OnKeyDown(Me, e)
End Sub

Private Sub TextField_KeyDown( _
ByVal sender As Object _
, ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles txtField.KeyDown
MyClass.OnKeyDown(sender, e)
End Sub

Looking at it you would not need OnKeyDown(sender, KeyEventArgs) you could
simply call TextField_KeyDown from OnKeyDown(KeyEventArgs) then call
MyBase.OnKeyDown fromt here...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top