views:

3790

answers:

2

I am working on a silverlight app that you need to enter information into a textbox and then just hit enter. Well there is no onclick event, that I could find, so what I did was use the onkeypressup event and check if it was the enter key that was pressed if so do "blah".

It just feels like there is a better way to do this. So the question is, is there?

+1  A: 

Do you really want it in the textbox? I would put a onkeyup handler on the container (e.g. Grid, Canvas) to press the button anywhere on the form.

Shawn Wildermuth
+3  A: 

I thinks that's the way to catch Key.Enter.

Also, you're code will be more readable if you use the KeyDown event instead of the KeyUp event.

If you only care about catching Key.Enter for a single control then your approach is correct.

You can also catch the Key.Enter for a group of related controls by using the KeyDown event of their container ("Event Bubbling").

ptio
To use Event Bubbling is a great idea!my book about SL: Bubbling events are events that travel up the containment hierarchy. For example,MouseLeftButtonDown is a bubbling event. It’s raised first by the element that is clicked. Next,it’s raised by that element’s parent, and then by that element’s parent, and so on, untilSilverlight reaches the top of the element tree.
Christian