By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

Keypress event back to caller from control

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"Tom" <to*@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #2

P: n/a
Is this for validation?

If so you could use the CausesValidation approach.

Schneider
"Eric Dreksler" <ericd AT accessoneinc DOT com> wrote in message
news:#r*************@TK2MSFTNGP10.phx.gbl...
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

"Tom" <to*@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Nov 20 '05 #3

P: n/a
"Tom" <to*@nospam.com> wrote in message news:<#r**************@TK2MSFTNGP12.phx.gbl>...
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/
Nov 20 '05 #4

P: n/a
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

"John Fiala" <jc********@hotmail.com> wrote in message
news:f2**************************@posting.google.c om...
"Tom" <to*@nospam.com> wrote in message

news:<#r**************@TK2MSFTNGP12.phx.gbl>...
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/

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.