none
Windows form: TextBox timer on button press RRS feed

  • Question

  • Hello,

    I'm trying to run a very simple program that whenever i press a button i see a timer from 0 to 10 inside a text box,

    but instead, i see an empty text box and after 10 seconds i see the number 10.

    can someone suggest what am i missing here ?

    Thanks ! 

    Here is the code:

    using System;
    using System.Threading;
    using System.Windows.Forms;


    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                
            }
            private void button1_Click(object sender, EventArgs e)
            {
                int i = 0;
                string x;
                while (i <= 10)
                {
                    x = i.ToString();
                    textBox1.Text = x;
                    Thread.Sleep(1000);
                    i++;
                }

            }

        }

    }


    • Moved by CoolDadTx Tuesday, January 14, 2020 5:15 PM Winforms related
    Tuesday, January 14, 2020 3:40 PM

All replies

  • Refresh the TextBox

    Like :

    // code ...

    textBox1.Text = x;
    textBox1.Refresh();
    // code ...

    • Proposed as answer by simonb549 Friday, January 17, 2020 8:59 AM
    Tuesday, January 14, 2020 3:49 PM
  • Hello,

    I would steer clear of Thread.Sleep which makes the form unresponsive and use Task.Delay which keeps the form responsive e.g.

    private async void button1_Click(object sender, EventArgs e)
    {
        var index = 0;
        var value = "";
        while (index <= 10)
        {
            value = index.ToString();
            textBox1.Text = value;
            await Task.Delay(1000);
            index++;
        }
    }
    The TextBox will have time to refresh itself.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, January 14, 2020 4:07 PM
    Moderator
  • Thanks a lot! Problem solved !
    Tuesday, January 14, 2020 6:42 PM
  • Hello,

    Can you take a minute and mark one or both replies as answers that assisted you which can help others with the same or similar question.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, January 14, 2020 7:42 PM
    Moderator
  • Hi Yuvi105,
    I am glad you have got your solution,we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 17, 2020 8:30 AM