locked
How to use the text in a TextBox when KeyDown event is triggered. RRS feed

  • Question

  • I'm working on my first windows app here and I'm trying to take whats typed into the TextBox and use that

    string as part of what I print out to my TextBlock. My program works fine for bClick(a button click),

    but for KeyDown event (when enter is pressed) it prints as though nameInput.Text is an empty string(and this

    doesn't appear to be so).

    An explanation of whats going on here and a solution on how to fix it would be greatly appreciated!

    private void bClick(object sender, RoutedEventArgs e) { greetingOutput.Text = "You ate " + nameInput.Text + " pistachios!!!"; nameInput.Text = ""; nameInput.Focus(Windows.UI.Xaml.FocusState.Programmatic); nameInput.SelectionStart = nameInput.Text.Length; } private void enter_pressed(object sender, KeyRoutedEventArgs e) { String s = nameInput.Text; { greetingOutput.Text = "You ate " + nameInput.Text + " pistachios!!!"; nameInput.Text = ""; nameInput.Focus(Windows.UI.Xaml.FocusState.Programmatic); nameInput.SelectionStart = nameInput.Text.Length; } }



    • Edited by slinhart Monday, October 7, 2013 5:38 PM
    Monday, October 7, 2013 5:38 PM

Answers

  • Hi,

    if i see it correct you do not process the Enter Key. So you are Clearing the Text with every Key stroke

    Try this

     private void enter_pressed(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key != VirtualKey.Enter)
                {
                    return;
                }
                String s = nameInput.Text;
                {
                    greetingOutput.Text = "You ate " + nameInput.Text + " pistachios!!!";
                    nameInput.Text = "";
                    nameInput.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                    nameInput.SelectionStart = nameInput.Text.Length;
                }
            }


    lh

    • Marked as answer by Anne Jing Tuesday, October 15, 2013 8:16 AM
    Monday, October 7, 2013 5:53 PM

All replies

  • Hi,

    if i see it correct you do not process the Enter Key. So you are Clearing the Text with every Key stroke

    Try this

     private void enter_pressed(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key != VirtualKey.Enter)
                {
                    return;
                }
                String s = nameInput.Text;
                {
                    greetingOutput.Text = "You ate " + nameInput.Text + " pistachios!!!";
                    nameInput.Text = "";
                    nameInput.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                    nameInput.SelectionStart = nameInput.Text.Length;
                }
            }


    lh

    • Marked as answer by Anne Jing Tuesday, October 15, 2013 8:16 AM
    Monday, October 7, 2013 5:53 PM
  • Hello slinhart, 

    Because in your code, in keydown event you blank your textbox's text.

      nameInput.Text = "";

    try to remove that line. May be that it works.

    Tuesday, October 8, 2013 4:43 AM