Set timer on Buttons

I have created some button dynamically now I want to show all these buttons one by one . how could i use delay function . I use thread.sleep in loop . but it make delay for all buttons but I want to make delay for each button. I am using C# visual studio 2012

for (int i = 0; i < 10; i++)
{               
   btn[i] = new Button();
   btn[i].Text = i.ToString();
   btn[i].Click += Form1_Click;
   this.flowLayoutPanel1.Controls.Add(btn[i]);
   System.Threading.Thread.Sleep(1000);
}  //closing of code 
Jon Skeet
people
quotationmark

You're currently blocking the UI thread - don't do that.

Instead, I suggest you use a timer. For example:

class Foo : Form
{
    private int nextButtonToShow = 0;
    private Timer timer;
    private Button[] buttons;

    internal Foo()
    {
        timer = new Timer();
        timer.Tick += ShowNextButton;
        timer.Interval = 1000;
        timer.Enabled = true;
    }

    private void ShowNextButton(object sender, EventArgs e)
    {
        // TODO: Set location etc
        Button button = new Button { Text = nextButtonToShow.ToString() };
        button.Click += ...;
        buttons[i] = button;
        Controls.Add(button);
        nextButtonToShow++;
        if (nextButtonToShow == buttons.Length)
        {
            timer.Enabled = false;
        }
    }
}

(You should make sure you dispose of the timer when the form is disposed, too.)

people

See more on this question at Stackoverflow