Home > Software engineering >  How to show each character 1 by one and display it as a label
How to show each character 1 by one and display it as a label

Time:11-17

I have tried to convert the char to ToString() but it only shows the last character of the sentence which is "N" from the admin. Basically i want to show the whole sentence by show each of the characters with the thread.

public partial class Form1 : Form
    {
        string quote;
        public Form1()
        {
            InitializeComponent();

           quote = "Tips: Sometimes just leave her, to pause. - Admin";
            for (int i=0; i < quote.Length; i  ){

               label1.Text =quote[i].ToString();
                Thread.Sleep(100);
            }
        }
    }
}

This it the output: enter image description here I did increase the length of the label too: enter image description here This is what I really want guys: enter image description here

CodePudding user response:

As commented you should use a winforms Timer to update the UI-thread every 100 ms:

private void Form1_Load(object sender, EventArgs e)
{
    System.Windows.Forms.Timer updateLabelTimer = new();
    string quote = "Tips: Sometimes just leave her, to pause. - Admin";        
    int currentIndex = 0;

    updateLabelTimer.Tick  = new EventHandler(UpdateLabelHandler);
    updateLabelTimer.Interval = 100;
    updateLabelTimer.Start();

    void UpdateLabelHandler(object? timerSender, EventArgs args)
    {
        label1.Text  = quote[currentIndex];
        if (  currentIndex == quote.Length)
        {
            updateLabelTimer.Enabled = false;
        }
    }
}
  • Related