数组,计时器和更改文本块颜色来回问题? [英] Arrays, timer and changing a textblocks color back and forth question?

查看:57
本文介绍了数组,计时器和更改文本块颜色来回问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在创建一个应用程序,其中有4个不同的文本块。它们将从白色到绿色1随机变换1秒,然后再变回白色。要闪存的文本块由存储在int [] lotto中的随机数确定。我遇到的问题是他们不会一次改变一个,他们(全部)在同一时间改变为绿色。任何帮助,将不胜感激。我尝试使用Thread.Sleep(1000)暂停代码,但它不起作用。



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();
        }

推荐答案

在每次调用的timer_Tick方法中,你遍历整个 lotto 数组并将所有对象设置为绿色。这就是为什么所有这些都在同时改变的原因。


如果你想让它们单独更改,你需要存储例如方法之外的计数器值和检查哪个控件每次重新着色。



例如你可以有一个变量

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;



在表单级别定义,并根据变量值的模数更改值。所以如果你有4个控件,比如


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++;
}



另外为什么不将控件存储在数组中。这样你就可以使用callcounter改变基于数组索引的颜色。



要将颜色更改回白色,只需使用相同的Tick事件并调查当前颜色。如果绿色变为白色,反之亦然。引入第二个计时器会增加不必要的复杂性。


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.


这篇关于数组,计时器和更改文本块颜色来回问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆