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 :

Ecrire texte lettre par lettre


Sujet :

C#

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 10
    Points : 8
    Points
    8
    Par défaut Ecrire texte lettre par lettre
    Bonjour,

    Je voudrais écrire une chaine dans un label lettre par lettre avec un espace de 1sec entre chaque lettre.

    Voici mon code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    string sTexte = utextBoxText.Text;
    char[] tableauLettres = sTexte.ToCharArray();
     
    foreach (char l in tableauLettres) 
    { 
    uLabelT.Text += l.ToString();
    Thread.Sleep(1000); 
    }
    Il ne m'affiche pas lettre par lettre mais il attend et actualise le Form (Et donc affiche la chaine complète) quand il sort de la boucle.

    Comment faire ?

  2. #2
    Membre confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2009
    Messages
    317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Février 2009
    Messages : 317
    Points : 560
    Points
    560
    Par défaut
    Pour se faire tu dois passer par un thread / BackgroundWorker pour affecter ton texte au label.
    Attention, tu risques d'avoir une alerte te disant que tu ne peux accéder à un contrôle créé par un autre thread, tu devras donc utiliser les Invoke...

    Voilà en gros ce que ca donne (j'ai testé) :

    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
     
    namespace BackgroundWorker_Exemple
    {
        public partial class Form1 : Form
        {
     
            private delegate String GetTextDelegateHandler();
            private GetTextDelegateHandler GetTextDelegate;
     
            private delegate void SetTextDelegateHandler(String texte);
            private SetTextDelegateHandler SetTextDelegate;
     
     
            public Form1()
            {
                InitializeComponent();
                GetTextDelegate = new GetTextDelegateHandler(GetText);
                SetTextDelegate = new SetTextDelegateHandler(SetText);
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
     
            }
     
            private void SetLabel()
            {
                string sTexte = textBox1.Text;
                char[] tableauLettres = sTexte.ToCharArray();
     
                foreach (char l in tableauLettres)
                {
                    String texte = (String)label1.Invoke(GetTextDelegate);
                    texte += l.ToString();
                    label1.Invoke(SetTextDelegate, texte);
                    Thread.Sleep(1000);
                }
            }
            private String GetText()
            {
                return label1.Text;
            }
            private void SetText(String texte)
            {
                label1.Text = texte;
            }
     
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                SetLabel();
     
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                backgroundWorker1.RunWorkerAsync();
     
            }
     
     
        }
    }

  3. #3
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 154
    Points : 25 072
    Points
    25 072
    Par défaut
    un timer est suffisant et plus simple qu'un backgroundworker !

    il suffit d'avoir un index et de lire la propriété indexée chars
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

  4. #4
    Membre confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2009
    Messages
    317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Février 2009
    Messages : 317
    Points : 560
    Points
    560
    Par défaut
    En effet x)

    Voici ce que cela donnerait avec un Timer (configuré avec un Tick à 1000) :

    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
     
    namespace BackgroundWorker_Exemple
    {
        public partial class Form1 : Form
        {
            int index = 0;
            char[] tableauLettres;
     
            public Form1()
            {
                InitializeComponent();
            }
     
     
     
     
            private void timer1_Tick(object sender, EventArgs e)
            {
                if(index <= tableauLettres.Length-1)
                {
                    label1.Text = label1.Text + tableauLettres[index];
                    index++;
                }
                else
                    timer1.Enabled = false;
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Length > 0)
                {
                    tableauLettres = textBox1.Text.ToCharArray();
                    timer1.Enabled = true;
                }
     
            }
     
     
        }
    }

Discussions similaires

  1. Réponses: 1
    Dernier message: 29/01/2017, 19h31
  2. [JavaScript] [jQuery, plugin] Afficher un texte lettre par lettre
    Par danielhagnoul dans le forum Contribuez
    Réponses: 6
    Dernier message: 08/12/2012, 22h38
  3. Creer un texte qui s'affiche lettre par lettre
    Par JCMANSION dans le forum Flash
    Réponses: 2
    Dernier message: 09/07/2010, 14h44
  4. Affichage texte lettre par lettre
    Par Yami no tenshi dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 03/09/2008, 13h22
  5. Afficher un texte lettre par lettre
    Par koKoTis dans le forum Flash
    Réponses: 2
    Dernier message: 11/12/2007, 21h14

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