Click here to Skip to main content
14,163,348 members
Rate this:
 
Please Sign up or sign in to vote.
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.

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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
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
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.
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190518.1 | Last Updated 14 Jul 2015
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100