Focus TextBox WPF
Résolu
Theolit
Messages postés
39
Date d'inscription
Statut
Membre
Dernière intervention
-
Utilisateur anonyme -
Utilisateur anonyme -
A voir également:
- Rodem notifier wpf
- Update notifier - Télécharger - Optimisation
- Mail notifier - Télécharger - Mail
- Outlook change notifier - Forum Outlook
- Comment notifier les visites sur facebook - Guide
- Update notifier magix - Forum Windows 10
2 réponses
Bonjour
J'ai un Grid que j'ai appellé "leGrid", avec 4 textbox, tous abonnés à la même méthode pour leur évènement KeyDown
Actuellement cela passe au suivant dans l'ordre où il apparaissent dans le xaml. C'est à dire, pour l'exemple, pas dans l'ordre où on s'y attend en les voyant sur l'interface.
Si tu veux forcer un ordre, tu peux par exemple te servir de la propriété Tag
J'ai un Grid que j'ai appellé "leGrid", avec 4 textbox, tous abonnés à la même méthode pour leur évènement KeyDown
<Grid x:Name="leGrid"> <TextBox HorizontalAlignment="Left" Height="23" Margin="445,69,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" KeyDown="TextBox_KeyDown"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="445,125,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" KeyDown="TextBox_KeyDown"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="445,153,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" KeyDown="TextBox_KeyDown"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="445,97,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" KeyDown="TextBox_KeyDown"/>
private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if(e.Key == System.Windows.Input.Key.Enter) { TextBox textBox = sender as TextBox; //on caste sender en textbox if (textBox == null)//si en fait sender n'était pas une textbox on sort return; List<TextBox> textBoxes = leGrid.Children.OfType<TextBox>().ToList();//la liste de tous les textbox actuels int index = textBoxes.IndexOf(textBox);//index du textbox en cours dans la liste if (++index == textBoxes.Count)//si een ajoutant 1, on atteint un index trop grand, on remet à zéro index = 0; textBoxes[index].Focus(); } }
Actuellement cela passe au suivant dans l'ordre où il apparaissent dans le xaml. C'est à dire, pour l'exemple, pas dans l'ordre où on s'y attend en les voyant sur l'interface.
Si tu veux forcer un ordre, tu peux par exemple te servir de la propriété Tag
<Grid x:Name="leGrid"> <TextBox HorizontalAlignment="Left" Height="23" Margin="445,69,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" KeyDown="TextBox_KeyDown" Tag="1"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="445,125,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" KeyDown="TextBox_KeyDown" Tag="3"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="445,153,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" KeyDown="TextBox_KeyDown" Tag="4"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="445,97,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" KeyDown="TextBox_KeyDown" Tag="2"/>
private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if(e.Key == System.Windows.Input.Key.Enter) { TextBox textBox = sender as TextBox; //on caste sender en textbox if (textBox == null)//si en fait sender n'était pas une textbox on sort return; List<TextBox> textBoxes = leGrid.Children.OfType<TextBox>().OrderBy(t => Convert.ToInt32(t.Tag)).ToList();//la liste de tous les textbox actuels int index = textBoxes.IndexOf(textBox);//index du textbox en cours dans la liste if (++index == textBoxes.Count)//si een ajoutant 1, on atteint un index trop grand, on remet à zéro index = 0; textBoxes[index].Focus(); } }
Edit: J'ai trouvé comment changer de TextBox automatiquement
Le seul problème maintenant c'est qu'il n'attend pas "Entrée" pour passer à la suivante
J'ai essayé avec un e.Key == Key.Enter mais ça ne fonctionne pas pour le moment
string TxtBox; for (int i = 1; i <= 4; ) { TxtBox = "Box" + i; Control c = (Control)Grid.FindName(TxtBox); c.Focus(); }
Le seul problème maintenant c'est qu'il n'attend pas "Entrée" pour passer à la suivante
J'ai essayé avec un e.Key == Key.Enter mais ça ne fonctionne pas pour le moment
Merci pour votre réponse, ça fonctionne nickel!