bored again...so i knocked up this dependency property class that allows you to attach to an existing form and set both the enter as tab properties and auto select on tab entry
for example if your text box in a grid
ref to namespace xmlns:my="clr-namespace:WpfApplication1DelMe"
then <Grid my:SmartText.AutoSelect="True" my:SmartText.EnterAsTab="True" Name="Grid1">
the autoselect can also be done at the textbox only level too
only lightly tested, but might be of interest to you anyway as demonstrates sly use of dependency props...
================= Create a new Class containing this code ===========
Public Class SmartText
#Region "EnterAsTab"
Public Shared Function GetEnterAsTab(ByVal obj As DependencyObject) As Boolean
GetEnterAsTab = obj.GetValue(EnterAsTabProperty)
End Function
Public Shared Sub SetEnterAsTab(ByVal obj As DependencyObject, ByVal value As Boolean)
obj.SetValue(EnterAsTabProperty, value)
End Sub
Public Shared Sub ue_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs)
Dim ue As FrameworkElement
ue = e.OriginalSource
If (e.Key = Key.Enter) Then
e.Handled = True
ue.MoveFocus(New TraversalRequest(FocusNavigationDirection.Next))
End If
End Sub
Private Shared Sub ue_Unloaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim ue As FrameworkElement
ue = sender
If (ue Is Nothing) Then Return
RemoveHandler ue.Unloaded, AddressOf ue_Unloaded
RemoveHandler ue.PreviewKeyDown, AddressOf ue_PreviewKeyDown
End Sub
Public Shared ReadOnly EnterAsTabProperty As DependencyProperty = DependencyProperty.RegisterAttached("EnterAsTab", GetType(Boolean), GetType(SmartText), New UIPropertyMetadata(False, AddressOf EnterAsTabChanged))
Public Shared Sub EnterAsTabChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim ue As FrameworkElement
ue = d
If (ue Is Nothing) Then Return
If (e.NewValue) Then
AddHandler ue.Unloaded, AddressOf ue_Unloaded
AddHandler ue.PreviewKeyDown, AddressOf ue_PreviewKeyDown
Else
RemoveHandler ue.PreviewKeyDown, AddressOf ue_PreviewKeyDown
End If
End Sub
#End Region
#Region "AutoSelect"
Public Shared Function GetAutoSelect(ByVal obj As DependencyObject) As Boolean
GetAutoSelect = obj.GetValue(AutoSelectProperty)
End Function
Public Shared Sub SetAutoSelect(ByVal obj As DependencyObject, ByVal value As Boolean)
obj.SetValue(AutoSelectProperty, value)
End Sub
Public Shared Sub ue_GotFocus(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim ue As TextBox = sender
ue.SelectAll()
End Sub
Private Shared Sub ue_AutoSelectContainerLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim pa As FrameworkElement = sender
RemoveHandler pa.Loaded, AddressOf ue_AutoSelectContainerLoaded
For i = 1 To VisualTreeHelper.GetChildrenCount(sender)
Dim dobj As DependencyObject = VisualTreeHelper.GetChild(sender, i - 1)
If TypeOf dobj Is TextBox Then
Dim txt As TextBox = dobj
AddHandler txt.Unloaded, AddressOf ue_AutoSelectUnloaded
AddHandler txt.GotFocus, AddressOf ue_GotFocus
End If
Next
End Sub
Private Shared Sub ue_AutoSelectUnloaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim ue As TextBox
ue = sender
If (ue Is Nothing) Then Return
RemoveHandler ue.Unloaded, AddressOf ue_AutoSelectUnloaded
RemoveHandler ue.GotFocus, AddressOf ue_GotFocus
End Sub
Public Shared ReadOnly AutoSelectProperty As DependencyProperty = DependencyProperty.RegisterAttached("AutoSelect", GetType(Boolean), GetType(SmartText), New UIPropertyMetadata(False, AddressOf AutoSelectChanged))
Public Shared Sub AutoSelectChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
If Not TypeOf d Is TextBox Then
If Not TypeOf d Is FrameworkElement Then Return
Dim pa As FrameworkElement = d
If (e.NewValue) Then
AddHandler pa.Loaded, AddressOf ue_AutoSelectContainerLoaded
Else
RemoveHandler pa.Loaded, AddressOf ue_AutoSelectContainerLoaded
For i = 0 To VisualTreeHelper.GetChildrenCount(pa)
Dim dobj As DependencyObject
dobj = VisualTreeHelper.GetChild(pa, i)
If TypeOf dobj Is TextBox Then
Dim txt As TextBox = dobj
RemoveHandler txt.GotFocus, AddressOf ue_GotFocus
End If
Next
End If
Return
Else
Dim ue As TextBox = d
If (e.NewValue) Then
AddHandler ue.Unloaded, AddressOf ue_AutoSelectUnloaded
AddHandler ue.GotFocus, AddressOf ue_GotFocus
Else
RemoveHandler ue.GotFocus, AddressOf ue_GotFocus
End If
End If
End Sub
#End Region
Private Shared Sub DumpLogicalTree(ByVal parent As Object, ByVal level As Integer)
Dim doParent As DependencyObject
Dim typeName As String = parent.GetType().Name
Dim name As String = Nothing
If (TypeOf parent Is DependencyObject) Then
doParent = parent
name = doParent.GetValue(FrameworkElement.NameProperty)
Trace.WriteLine(String.Format("{0}: {1}", typeName, name))
For Each child In LogicalTreeHelper.GetChildren(doParent)
DumpLogicalTree(child, level + 1)
Next
Else
name = parent.ToString()
Trace.WriteLine(String.Format("{0}: {1}", typeName, name))
Return
End If
End Sub
End Class