I'm doing a prototype c# program to test out a concept I have. The prototype is a simple for loop (10x) that sets a textbox background color property to a randomly generated color and then waits 2 seconds and does it again.
for (int i = 0; i < 10; i++)
{
Color NewColor = GetRandomColor();
tbLight1.BackColor = NewColor;
Application.DoEvents();
WaitNSeconds(2);
}
Using the debug feature I know that the random color generator works and the code does a loop through 10 times. However, I only see the final color in the text box. I have tried Refresh(), Update(), Invalidate(), and now Application.DoEvents. They all result in the same. I only see the final color, not the 9 before it. I don't know what else to try.
Here is the WaitNSeconds() method. This I got from stackoverflow. I'm going to use Thread.Sleep(2000); instead.
private void WaitNSeconds(int segundos)
{
if (segundos < 1) return;
DateTime _desired = DateTime.Now.AddSeconds(segundos);
while (DateTime.Now < _desired)
{
System.Windows.Forms.Application.DoEvents();
}
}
Note: Since this has been flagged as a duplicate, I have moved to that question to proceed. Thanks!