Keypress event back to caller from control

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I have a VB.NET user control that I wrote - this control has three or four
other controls on it (textbox, combobox, datetime picker, etc). Now,
whenever the control is on a form and the user enters anything into the
textbox (for instance) I trap the keypress event to handle some stuff (i.e.
if it is an enter key, etc). Now, once I am done with handling the keypress
event for the textbox, I then need to pass that keypress event BACK to the
main form that is hosting the control. I can't just define another KeyPress
event and try a RaiseEvent because the control already has a keypress event.

I must be brain dead or something today, because normally I should be able
to figure this out but it just isn't coming to me. Once I am done with the
KeyPress event in my control, how can I pass that event (and it's data -
i.e. what key was pressed) back to the host form?

Tom
 
Can you override the controls KeyPress event in the parent form, and then
just call the KeyPress event for the parent form when your done?

Or else raise a public event in the control and catch it in the parent form
(passing along
(ByVal sender As Object, ByVal e As System.EventArgs)) info and then call
the KeyPress event in the parent?

public class MyControl()

Public Event OnTextBox1KeyPress (ByVal sender as Object, ByVal e As
System.EventArgs)

Private Sub TextBox1_KeyPress (ByVal sender as Object, ByVal e As
System.EventArgs)

'Do something

RaiseEvent OnTextBox1KeyPress (sender,e)

End Sub

End Class



Public Class MyForm

Dim testcontrol as New MyControl

Private Sub testcontrol_KeyPressHandler (ByVal sender as Object, ByVal
e As System.EventArgs) _ Handles testcontrol_OnTextBox1KeyPress

MyForm_KeyPress (sender,e)

End Sub



Private Sub MyForm_KeyPress (ByVal sender as Object, ByVal e As
System.EventArgs) Handles MyForm.KeyPress

'Do Something

End Sub

End Class

I'm sure there is a better way, but this should work fairly easy as well.

Eric Dreksler
 
Is this for validation?

If so you could use the CausesValidation approach.

Schneider
 
Tom said:
I must be brain dead or something today, because normally I should be able
to figure this out but it just isn't coming to me. Once I am done with the
KeyPress event in my control, how can I pass that event (and it's data -
i.e. what key was pressed) back to the host form?

Tom

Do you need to return the sender as well? Otherwise, calling
<control>.OnKeyPress(e), where e is the event data object you
intercepted, should do it, right?

John Fiala
jcfiala523-at-hotmail.com
http://www.livejournal.com/users/fiala_tech/
 
Actually the form will get the event first if the Form.KeyPreview is set.

if you set the "e.handled=True" in the form, i think the control never gets
it.

Schneider
 
Back
Top