Use Keyboard.Focus to set the focus to a Text Field : Key Event « Windows Presentation Foundation « VB.Net






Use Keyboard.Focus to set the focus to a Text Field

Use Keyboard.Focus to set the focus to a Text Field
    


<Window 
  x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1" Height="600" Width="800">
  <DockPanel>
    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
      <TextBox x:Name="tbxInsertionText" Width="200" Margin="5,0" />
      <Button DockPanel.Dock="Bottom" Content="Insert" Click="btnInsert_Click"/>
    </StackPanel>
    <RichTextBox x:Name="rtbTextContent" />
  </DockPanel>
</Window>
//File:Window.xaml.vb
Imports System.Windows
Imports System.Windows.Documents
Imports System.Windows.Input

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub

    Private Sub btnInsert_Click(sender As Object, e As RoutedEventArgs)
      If String.IsNullOrEmpty(tbxInsertionText.Text) Then
        Return
      End If
      rtbTextContent.BeginChange()
      If rtbTextContent.Selection.Text <> String.Empty Then
        rtbTextContent.Selection.Text = String.Empty
      End If
      Dim tp As TextPointer = rtbTextContent.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward)
      rtbTextContent.CaretPosition.InsertTextInRun(tbxInsertionText.Text)
      rtbTextContent.CaretPosition = tp
      rtbTextContent.EndChange()
      Keyboard.Focus(rtbTextContent)
    End Sub
  End Class
End Namespace

   
    
    
    
  








Related examples in the same category

1.Creating a KeyBinding between the Open command and Ctrl-RCreating a KeyBinding between the Open command and Ctrl-R
2.Command Handler Key BindingCommand Handler Key Binding
3.Provide Quick Keyboard Access to ButtonsProvide Quick Keyboard Access to Buttons
4.Reading keyboard modifiersReading keyboard modifiers
5.Reading individual key state with Keyboard.IsKeyDownReading individual key state with Keyboard.IsKeyDown
6.Use InputBinding to bind Application commends to Key eventsUse InputBinding to bind Application commends to Key events
7.Suppress Keyboard and Mouse EventsSuppress Keyboard and Mouse Events
8.Query Left / Right control keyQuery Left / Right control key
9.Is Key.CapsLock ToggledIs Key.CapsLock Toggled
10.Keyboard.IsKeyToggledKeyboard.IsKeyToggled
11.Query Number lock keyQuery Number lock key
12.Use KeyBinding to bind Key event to TextBox.InputBindingsUse KeyBinding to bind Key event to TextBox.InputBindings
13.Custom Command by KeyGesture and RoutedUICommandCustom Command by KeyGesture and RoutedUICommand
14.TextBox PreviewKeyDown, PreviewKeyUp, PreviewTextInput, KeyDown, KeyUp and TextChanged eventsTextBox PreviewKeyDown, PreviewKeyUp, PreviewTextInput, KeyDown, KeyUp and TextChanged events
15.StackPanel PreviewTextInput and PreviewKeyDownStackPanel PreviewTextInput and PreviewKeyDown
16.If input is not a number do not handle the key eventIf input is not a number do not handle the key event
17.RoutedEvents: Tunneled Key PressRoutedEvents: Tunneled Key Press
18.On Key Down HandlerOn Key Down Handler