WPF war of the set focus()

This topic contains 1 reply, has 1 voice, and was last updated by  Steve 1 week, 1 day ago.

  • Author
    Posts
  • #83752

    Steve
    Participant

    Hello everyone,

    got bit of a tickler here..

    I have two controls warring for focus and I'm not sure how to get all my cake and eat it too.
    Given that the cake is a lie, I present you with this conundrum:

        $syncHash.OutputTextbox.Add_TextChanged({
            $syncHash.OutputTextbox.SelectionStart=$syncHash.OutputTextbox.Text.Length
            $syncHash.OutputTextbox.ScrollToCaret()
            $syncHash.OutputTextbox.Focus()
        })
    

    I also have this:

        $syncHash.window.add_KeyDown({
            param(
                [Parameter(Mandatory)][Object]$sender,
                [Parameter(Mandatory)][Windows.Input.KeyEventArgs]$e
            )
            if($e.Key -eq 'Enter'){
                $syncHash.Window.Dispatcher.invoke("Background",[action]{$syncHash.OutputTextBox.AppendText((Send-RTCmd|Out-String))});
                $syncHash.InputTextBox.Focus();
                }
        })
    

    I just added the set focus to the Input which pretty much capped the finishing touches on this application only to have the output textbox no longer scroll to the bottom.

    I tried chaining the commands so that the enter key had all the Output textbox event commands first, and the input textbox focus last, but that didn't seem to work. The cursor ended up in the input textbox ( which is fine ) but it didn't scroll the output to the end.

    I also tried leaving out the focus command from the output, deciding it wasn't necessary – it is, or seems to be according to my testing.

    Suggestions?

    Thank you

  • #83986

    Steve
    Participant

    I Resolved this by using Boe Prox's TextBlock example

    In the Xaml section:

    Sorry for plain text the form kept eating this section of the code (5 times).

    ScrollViewer Name = "scrollviewer" VerticalScrollBarVisibility="Visible" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="5,5,5,5" >
    TextBox Name="Router_Group_TextBox" IsReadOnly="True" Visibility="Hidden" TextWrapping="Wrap"/>
    /ScrollViewer >

    Then in the scripting sction:

    $syncHash.OutputTextbox.Add_TextChanged({
        $syncHash.scrollviewer.Dispatcher.Invoke("Normal",[action]{$syncHash.scrollviewer.ScrollToEnd()})
    })
    

    So that takes care of the Textbox autoscrolling to the bottom.

    For the Command window the following is all that is necessary

        $syncHash.window.add_KeyDown({
            param(
                [Parameter(Mandatory)][Object]$sender,
                [Parameter(Mandatory)][Windows.Input.KeyEventArgs]$e
            )
            if($e.Key -eq 'Enter'){
                $syncHash.Window.Dispatcher.invoke("Background",[action]{$syncHash.OutputTextBox.AppendText((Send-RTCmd|Out-String))});
                $syncHash.InputTextbox.Focus();
                }
        })
    

You must be logged in to reply to this topic.