Focus TextBox WPF

Résolu/Fermé
Theolit Messages postés 40 Date d'inscription mercredi 18 mars 2020 Statut Membre Dernière intervention 20 avril 2021 - Modifié le 27 avril 2020 à 11:21
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 - 28 avril 2020 à 10:29
Bonjour à tous, ✌

Mon but et de créer une application WPF avec plusieurs TextBox.
Quand je suis dans la première TxtBox et que je clique sur "Entrée" , j'aimerais que le curseur passe automatiquement à la TxtBox suivante etc.. Sans devoir faire un maTxtBox.Focus(); à chaque fois.

J'ai essayé pas mal de chose avec une incrémentation et Tabindex ou encore avec des string que j'essaye de passer en objet (non je n'ai pas réussi) mais pour le moment rien ne fonctionne.
Si l'un d'entre vous a une idée , je suis preneur.

Merci d'avance

2 réponses

Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 931
27 avril 2020 à 18:00
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
    <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();
            }
        }

1
Theolit Messages postés 40 Date d'inscription mercredi 18 mars 2020 Statut Membre Dernière intervention 20 avril 2021
Modifié le 28 avril 2020 à 08:04
Bonjour,

Merci pour votre réponse, ça fonctionne nickel!
0
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 931 > Theolit Messages postés 40 Date d'inscription mercredi 18 mars 2020 Statut Membre Dernière intervention 20 avril 2021
28 avril 2020 à 10:29
de rien
0
Theolit Messages postés 40 Date d'inscription mercredi 18 mars 2020 Statut Membre Dernière intervention 20 avril 2021
Modifié le 27 avril 2020 à 12:39
Edit: J'ai trouvé comment changer de TextBox automatiquement
 
            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
0