locked
Trouble getting a button to set the keyboard focus (from a nonprogrammer) RRS feed

  • Question

  • I am using Blend 4 + Sketchflow to create a WPF prototype. On a page, I want two things to happen: 1) On initial load, the focus appears in a particular textbox, and 2) when the user clicks a particular button, the focus moves to another control, a RichTextBox.

    I have gotten the first part to work, but not the second. I referred to this page:

    http://msdn.microsoft.com/en-us/library/aa969768.aspx 

    ...and got the "Keyboard.Focus" command to use in the cs file. I selected the page itself in the Objects & Timeline panel, double-clicked in the Loaded field in the Events panel under Properties, and ended up with this working code in the .cs file:

    private void Screen_1_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
       Keyboard.Focus(Textbox_1);
    }

    So far, so good - the keyboard focus is now on that textbox when the page first loads. But now, I have a button on this page, and I want a click of the button to move the keyboard focus to the RichTextbox. I selected "Button_1" on the Objects & Timeline, went through a similar process, and got this in my .cs file:

    private void Button_1_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        Keyboard.Focus(RichTextBox_1);
    }

    But it does not work - nothing happens in the resulting prototype when I click on "Button_1"  I initially double-clicked on the "Click" event field rather than the "MouseLeftButtonUp" field, but that didn't work either. I have made sure that I am referencing the name of the RichTextBox correctly, and that it is visible, enabled, focusable, and not read-only.

    Any ideas as to what could be going wrong?

    Thanks in advance!

    BMeyer

    Friday, November 9, 2012 7:22 PM

Answers

  • Button does not raise MouseLeftButtonUp, it raises Click instead. The button itself swallows MouseLeftButtonUp, can you try again with Click?
    • Marked as answer by BMeyerMcK Friday, November 9, 2012 7:47 PM
    Friday, November 9, 2012 7:34 PM
    Moderator

All replies

  • Button does not raise MouseLeftButtonUp, it raises Click instead. The button itself swallows MouseLeftButtonUp, can you try again with Click?
    • Marked as answer by BMeyerMcK Friday, November 9, 2012 7:47 PM
    Friday, November 9, 2012 7:34 PM
    Moderator
  • Well, going back and changing it back to Click fixed the problem - I must have had another problem going on when I had it on Click previously and it didn't work, which got fixed in the interim.  Thanks for the info on MouseLeftButtonUp, that's good to know!
    Friday, November 9, 2012 7:47 PM