Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Calling Focus method from TextBox to another Controls (TextBox, ComboBox or DatePicker) not work at all

$
0
0

I try many things but I'm always stuck on this, that's why I'm calling for help.

I have many controls on a UserControl in this order DatePicker named datDateRemplissage, 3 TextBox named respectivelyq6a, q6b and q6c (to enter an age), then a ComboBox namedq7.

<Label x:Name="labDateRemplissage" Content="Date De Remplissage: " Margin="0,10,0,0" Style="{StaticResource LabelOnTab}"/><DatePicker x:Name="datDateRemplissage" Width="177" Margin="0,10,0,0" Style="{StaticResource InputOnTab}" SelectedDateChanged="datDateRemplissage_SelectedDateChanged"/><Label x:Name="labQ6a" Grid.Row="0" Grid.Column="0" Style="{StaticResource LabelOnTab}" Foreground="AliceBlue" Content="Q6A. Age:" Height="28" Margin="10,176,292,17"/><TextBox x:Name="q6a" Grid.Row="0" Grid.Column="0" MaxLength="2" Style="{StaticResource InputOnTab}" HorizontalAlignment="Left" Height="23" Margin="89,178,0,0" VerticalAlignment="Top" Width="42" PreviewTextInput="TextBox_PreviewTextInput" DataObject.Pasting="TextBox_Pasting" LostFocus="q6a_LostFocus"/><Label x:Name="labQ6b" Grid.Row="0" Grid.Column="0" Foreground="AliceBlue" Style="{StaticResource LabelOnTab}"  Content="Q6B. Age:" Height="28" Margin="145,176,160,14"/><TextBox x:Name="q6b" Grid.Row="0" Grid.Column="0" MaxLength="2" Style="{StaticResource InputOnTab}" HorizontalAlignment="Left" Height="23" Margin="214,178,0,0" VerticalAlignment="Top" Width="42" PreviewTextInput="TextBox_PreviewTextInput" DataObject.Pasting="TextBox_Pasting" LostFocus="q6b_LostFocus"/><Label x:Name="labQ6c" Grid.Row="0" Grid.Column="0" Foreground="AliceBlue" Style="{StaticResource LabelOnTab}"  Content="Q6c. Age:" Height="28" Margin="256,176,50,14"/><TextBox x:Name="q6c" Grid.Row="0" Grid.Column="0" MaxLength="2" Style="{StaticResource InputOnTab}" HorizontalAlignment="Left" Height="23" Margin="319,178,0,0" VerticalAlignment="Top" Width="42" PreviewTextInput="TextBox_PreviewTextInput" DataObject.Pasting="TextBox_Pasting" LostFocus="q6c_LostFocus"/><Label x:Name="labQ7" Grid.Row="0" Grid.Column="1" Foreground="AliceBlue" Style="{StaticResource LabelOnTab}"  Content="Q7. Département:" Height="28" Margin="10,0,246,191"/><ComboBox x:Name="q7" Grid.Row="0" Grid.Column="1" Style="{StaticResource InputOnTab}" HorizontalAlignment="Left" Height="23" Margin="160,0,0,0" VerticalAlignment="Top" Width="202" IsReadOnly="True" DisplayMemberPath="Libelle" SelectionChanged="q7_SelectionChanged"><ComboBox.Resources><SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="orange" Opacity="0.6"/></ComboBox.Resources><ComboBox.ItemsSource><Binding Source="{StaticResource DepartementViewSource}"/></ComboBox.ItemsSource></ComboBox>


now when user select a date from the DatePicker, in code behind, I make some check and set Focus onq7. and It's worked

But When on q6a_LostFocus Event Handler, I check if age = 0 year then to force user input it in month, so I callq6c.Focus() Else (if age is not 0) I set focus on q7 (comboBox)q7.Focus()

and when after inter 0 in q6a TextBox and press Tab key, the focus is always set on the next TextBox, which is in this casq6b even though I enter an age above 0 the focus is not set to q7 but to q6b again as if the order of Controls in the UserControl has priority on the code behind setting.

'Code of controls : q6a : Age in year
Private Sub q6a_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        q6a.Text = q6a.Text.Replace(" ", "") 'Delete all spaces
        If Not String.IsNullOrEmpty(q6a.Text) Then
            q6a.Text = CInt(q6a.Text) 
            q6b.Text = ""
            q6c.Text = ""

            Dim age As Integer = q6a.Text

            'if age is 0 year, user must enter it in month in q6c TextBox
            If age = 0 Then
                Dim Erreur As String = "If age of the Item is less than 1 year" & Chr(13) & _"Specify it in months please."
                Select Case MessageBox.Show(Erreur, "Incorrect Informations", MessageBoxButton.OK, MessageBoxImage.Error)
                    Case MessageBoxResult.OK
                        q6c.Focus()
                        q6a.Text = ""
                End Select

            Else  'set focus on q7
                'Keyboard.Focus(q7)
                q7.Focus()
            End If
        End If
End Sub

What else can I try?

your helps are welcome. 



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>