IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Récupérer le texte de chaque textbox


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2017
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2017
    Messages : 42
    Par défaut Récupérer le texte de chaque textbox
    Bonjour à tous !

    Je suis bloqué dans mon code, je n'arrive pas à trouver comment récupérer plusieurs textbox en une fois.

    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <Window x:Class="Isia.AddClassIsia.FileNameDialog"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Ajouter une classe" Height="auto" Width="431" ResizeMode="CanResize" ShowInTaskbar="True" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight">
     
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="26" />
                <RowDefinition Height="26" />
                <RowDefinition Height="26" />
                <RowDefinition Height="26" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
     
            <Label Grid.Row="0" Grid.Column="0" Name="lblFolder" Content="Nom de la classe:" HorizontalAlignment="Left" Margin="5 0 0 0" VerticalAlignment="Center" Height="26" FontWeight="SemiBold" />
            <TextBox Grid.Row="0" Grid.Column="1" Name="txtName" VerticalContentAlignment="Center" Height="26" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Stretch" MinWidth="210"/>
            <Button Grid.Row="0" Grid.Column="3" Content="Ajouter" HorizontalAlignment="Center" Margin="10,0" VerticalAlignment="Center" Width="75" Height="26" IsDefault="True" Name="btnCreate" Click="Button_Click" />
     
                <Button Grid.Row="1" Grid.Column="0" Content="Ajouter une autre classe" HorizontalAlignment="Left" Margin="10,0" VerticalAlignment="Center" Width="auto" Height="26" IsDefault="False" Name="btnMoreClass" Click="BtnMoreClass_Click" />
                <Button Grid.Row="1" Grid.Column="3" Content="Valider" HorizontalAlignment="Left" Margin="10,0" VerticalAlignment="Center" Width="auto" Height="26" IsDefault="False" Name="btnLessClass" Click="BtnLessClass_Click" />
     
            <StackPanel Name="someStackPanel" Grid.Row="2" Grid.ColumnSpan="2" >
            </StackPanel >
     
            <CheckBox x:Name="LiaisionTable" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Margin="5,0,0,0" Content="Table de liaison (Ne pas cocher si ajout multiple)" VerticalAlignment="Center"/>
            <CheckBox x:Name="ViewModel" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="5,0,0,0" Content="Inclure les ViewModels" VerticalAlignment="Center"/>
     
            <Label Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" Name="lblTips" Content="Tips" HorizontalAlignment="Left" Margin="0,0" VerticalAlignment="Bottom" />
            <Label Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="3" Name="lblTips2" Content="Tips" HorizontalAlignment="Left" Margin="0,0" VerticalAlignment="Bottom" />
     
        </Grid>
     
    </Window>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    private void BtnMoreClass_Click(object sender, RoutedEventArgs e)
            {
                i_input++;
     
                TextBox txt = new TextBox
                {
                    Name = "textBox1"+ i_input
                };
     
                this.someStackPanel.Children.Add(txt);
                this.someStackPanel.RegisterName(txt.Name, txt);
            }
     
            private void BtnLessClass_Click(object sender, RoutedEventArgs e)
            {
                TextBox txt = (TextBox)this.someStackPanel.FindName("textBox1");
                if (txt != null)
                {
                    string message = string.Format("The number is {0}", txt.Text);
     
                    MessageBox.Show(message);
                }
                else
                {
                    MessageBox.Show("Textbox is null");
                }
            }
    J'essai dans une fenêtre, lorsque l'utilisateur clique sur le bouton Ajouter une autre classe, d'ajouter une textbox.
    Si il clique plusieurs fois, ça va lui ouvrir autant de textbox qu'il aura cliqué.
    Le soucis, c'est que pour une textbox, j'arrive à récupérer sa valeur, mais si il clique genre 2 fois, je ne vois pas comment récupérer chaque textbox.
    En JS, les input auraient la même classe, et avec une boucle je peux tout récupérer, mais la je vois pas comment faire.
    Si quelqu'un peut m'aider

    Merci d'avance !

  2. #2
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour

    Ton code dans la méthode BtnMoreClass_Click comporte un énorme raté à corriger.
    On récolte ce qu'on semé dans tout code...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
     
    private void BtnMoreClass_Click(object sender, RoutedEventArgs e)
            {
                i_input++;
     
                TextBox txt = new TextBox
                {
                    // 1er "flaw" ou raté
                    // a virer  :textBox+i_input =>textBox11 
                    //Name = "textBox1" + i_input,
     
                    Name = "textBox" + i_input,
                    // a rajouter
                    Text = i_input.ToString ()
                };
     
                this.someStackPanel.Children.Add(txt);
                this.someStackPanel.RegisterName(txt.Name, txt);
            }
     
            private void BtnLessClass_Click(object sender, RoutedEventArgs e)
            {
                // fatalement tu ne le trouveras jamais !!! 
                TextBox txt = (TextBox)this.someStackPanel.FindName("textBox1");
                if (txt != null)
                {
     
                    string message = string.Format("The number is {0}", txt.Text);
                    MessageBox.Show(message);
                }
                else
                {
                    MessageBox.Show("Textbox is null");
                }
            }
    bon code...

  3. #3
    Membre actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2017
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2017
    Messages : 42
    Par défaut
    Re, oui j'avais pas modifié avant de poster, c'était un test que j'effectuais. J'ai tenté de mettre des Name unique en concaténant avec une itération, mais bon, mon soucis ne vient pas de là. Je cherche à récupérer toutes les valeurs des Textbox en une fois lors de la soumission.

    Edit : J'ai trouvé

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    /// <summary>
            /// Ajout de texbox à la demande
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void BtnMoreClass_Click(object sender, RoutedEventArgs e)
            {
                i_input++;
     
                TextBox txt = new TextBox();
                txt.Name = "textBox" + i_input;
                txt.Width = 50;
                Grid.SetRow(txt, 0);
     
                this.someStackPanel.Children.Add(txt);
     
                this.someStackPanel.RegisterName(txt.Name, txt);
            }
     
            /// <summary>
            /// Validation des classes
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void BtnLessClass_Click(object sender, RoutedEventArgs e)
            {
                List<string> inputTexbox = new List<string>();
     
                foreach (UIElement pnl in someStackPanel.Children)
                {
                    if (pnl is TextBox)
                    {
                        var _texbox = pnl as TextBox;
                        inputTexbox.Add(_texbox.Text);
                    }
                }
            }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [RegExp] Récupérer du texte entre chaque indice
    Par bygleader dans le forum Général JavaScript
    Réponses: 10
    Dernier message: 25/01/2018, 11h45
  2. Récupérer le texte de chaque ligne dans un div style break-word
    Par patricktoulon dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 08/06/2017, 17h54
  3. récupérer le .text d'un textbox
    Par hirochirak dans le forum ASP.NET
    Réponses: 2
    Dernier message: 05/12/2008, 16h34
  4. [C#] Récupérer le texte de textBox créées dynamiquement
    Par cyllix dans le forum Windows Forms
    Réponses: 4
    Dernier message: 19/06/2006, 11h45
  5. Réponses: 13
    Dernier message: 11/05/2006, 14h15

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo