Author Posts

November 7, 2017 at 6:56 pm

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

November 11, 2017 at 8:26 pm

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();
            }
    })