UserControl not getting keypress

A

Andy

Hi,

I have a user control which has a text box and button on it. I'm
trying to have the same code fire when the user presses Enter in the
text box as would have if the user clicked the button.

The control is on a form which has acceptButton defined. The problem
is that when I press enter, the form containing the control closes
(because it 'clicks' the OK button) and the text box never even gets a
chance to process and handle the key press.

Any ideas?

Thanks
Andy
 
S

Stoitcho Goutsev \(100\)

The easiest way would be to remove accept button. Then the enter will go to
the editbox. I guess you don't won't to do that. There is a workaround if
you don't won't to write code
1. Set the Multiline property for the textbox to *true*
2. Set AcceptsReturn of the textbox to *true*
3. Handle the KeyPressed event and if the key is the enter key ('\r') set
Handled property of the event arguments to *true* this way you'll stop the
editbox of adding the new line and it will work as single-line.
 
A

Andy

I'll remove the accept button, since the workaround wouldn't work in my
case.

Thanks for the tip.

Andy
 

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