views:

84

answers:

1

Hi. I want to capture the text from the textbox when enter key is hit. I am using WPF/visual studio 2010/.NET 4. I dont know what event handler to be used in the tag ? I also want to do the same for maskedtextbox.

+2  A: 

Either KeyDown or KeyUp.

TextBox tb = new TextBox();
tb.KeyDown += new KeyEventHandler(tb_KeyDown);

static void tb_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        //enter key is down
    }
}
tafa
how do i do it for the maskedtextbox? its a radmaskedtextbox
VP
the radmaskedtextbox does not givee an option of "AcceptsReturn" inside the tag..
VP
Nothing would be different. MaskedTextBox has the same eventhandlers. In fact all controls have them.
tafa
this handler however gets fired when any key sis pressed. I want to fire the event only when enter is pressed. and then I want to collect whats in the textbox. so here is what it does. the user types in a name and presses enter. thats when i want to fire the event. and not while the user is typing his name..i hope this explains it..
VP
You are right. That's why in the example I gave, there is an if statement checking that condition in the handler method. This is how KeyEventHandlers are designed to work. Unfortunately there is no easy way to do what you want, I do not know if there is any.
tafa