Usercontrol : how to intercept "Enter" (originally asked in windowsforms.controls group).

S

Simon Verona

Sorry for the repost, but this group seems to be more "active" than the
group that I posted my question, and it's probably as valid here as there!

I have a usercontrol, which contains a textbox (as well as other controls).
The textbox is set for multiline, so it *should* accept an Enter Key to move
down to the next line.

However, I'm finding that if I put the control onto a form which has a
default Accept button set, then pressing enter when in the multi-line text
box will fire the click event on the default accept button, not create a new
line in the multi-line textbox.

I'm guessing I need to capture the enter key in the control before it gets
passed back up to the form and the default button gets processed.

How do I do this?

I'm writing in vb.net / framework 1.1.

Thanks in advance
Simon Verona
 
C

cfps.Christian

Sorry for the repost, but this group seems to be more "active" than the
group that I posted my question, and it's probably as valid here as there!

I have a usercontrol, which contains a textbox (as well as other controls).
The textbox is set for multiline, so it *should* accept an Enter Key to move
down to the next line.

However, I'm finding that if I put the control onto a form which has a
default Accept button set, then pressing enter when in the multi-line text
box will fire the click event on the default accept button, not create a new
line in the multi-line textbox.

I'm guessing I need to capture the enter key in the control before it gets
passed back up to the form and the default button gets processed.

How do I do this?

I'm writing in vb.net / framework 1.1.

Thanks in advance
Simon Verona

My train of thought would be to find out which event is fired first
the KeyDown event in the control/form even. Once you have that you
can see if your box has focus and act accordingly OR you can turn off
the accept / cancel button in the textbox.GotFocus event and turn them
back on in the LostFocus event. This way the enter key would be used
in your textbox correctly and when the focus is lost everything
returns to normal.
 
S

Simon Verona

Thanks for the idea, it worked a treat.

I ended up by picking up the gotfocus event on the text box in the
usercontrol, and checking the parentform.acceptbutton, saving it's contents
in an object variable, and then making parentform.acceptbutton=nothing. I
then simply restore parentform.acceptbutton to it's previous value in the
lostfocus event.

Regards
Simon
 
A

Armin Zingler

Simon Verona said:
Thanks for the idea, it worked a treat.

I ended up by picking up the gotfocus event on the text box in the
usercontrol, and checking the parentform.acceptbutton, saving it's
contents in an object variable, and then making
parentform.acceptbutton=nothing. I then simply restore
parentform.acceptbutton to it's previous value in the lostfocus
event.

Why not set the textbox' AcceptsReturn property to True?


Armin
 

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