Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I am creating an application where I have 4 different textblocks. They are to randomly change from the color white to the color green 1 at a time for 1 second and then back to white. The textblock that is to flash is determined by a random number stored in the int[] lotto. The problem I am encountering is that they don't change one at a time, they (all) change at exactly the same time to green. Any help would be appreciated. I tried using Thread.Sleep(1000) to pause the code but it does not work.

C#
DispatcherTimer timer = new DispatcherTimer();
        DispatcherTimer timer3 = new DispatcherTimer();
       
        public RoundPage()
        {
            InitializeComponent();

            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer_Tick;
            timer.Start();

            timer3.Interval = TimeSpan.FromSeconds(1);
            timer3.Tick += timer3_Tick;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < lotto.Length; i++)
            {
                if (lotto[i].ToString() == "0")
                {
                    UpArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Green);
                    Thread.Sleep(1000);
                    timer3.Start();
                }

                else if (lotto[i].ToString() == "1")
                {
                    DownArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Green);
                    Thread.Sleep(1000);
                    timer3.Start();
                }

                else if (lotto[i].ToString() == "2")
                {
                    LeftArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Green);
                    Thread.Sleep(1000);
                    timer3.Start();
                }

                else if (lotto[i].ToString() == "3")
                {
                    RightArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Green);
                    Thread.Sleep(1000);
                    timer3.Start();
                }
                timer.Stop();
            }
        }

        void timer3_Tick(object sender, EventArgs e)
        {

            UpArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.White);
            DownArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.White);
            LeftArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.White);
            RightArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.White);
            timer3.Stop();
        }
Posted
Updated 13-Jul-15 18:35pm
v2

1 solution

Inside the timer_Tick method you on each call you loop through the whole lotto array and you set all the objects to green. This is why all of them are changing at the same time.

If you want them to change individually, you need to store for example a counter value outside the method and check which control to re-color each time.

For example you could have a variable
C#
private int callcounter = 0;

defined on the form level and change the value based on the modulo of the variable value. So if you have 4 controls, something like
C#
void timer_Tick(object sender, EventArgs e)
    {
            if (lotto[(callcounter % 4)].ToString() == "0")
            {
                UpArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Green);
            }
 
            else if (lotto[(callcounter % 4)].ToString() == "1")
            {
                DownArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Green);
            }
 
            else if (lotto[(callcounter % 4)].ToString() == "2")
            {
                LeftArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Green);
            }
 
            else if (lotto[(callcounter % 4)].ToString() == "3")
            {
                RightArrow.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Green);
            }
            callcounter++;
}

Also why not store the controls in the array.This way you could just change the color of based on the index of the array using the callcounter.

To change the color back to whit, just use the same Tick event and investigate the current color. If green change to white and vice versa. Introducing a second timer adds unnecessary complexity.
 
Share this answer
 
v2
Comments
CBO1987 14-Jul-15 11:18am    
Thank you very much, that helped a lot!
Wendelius 14-Jul-15 23:46pm    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900