Timer and Color Reset

Page 1 of 1

3 Replies - 4514 Views - Last Post: 15 December 2013 - 10:05 PM Rate Topic: -----

#1 codingnoob13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-October 13

Timer and Color Reset

Posted 15 December 2013 - 01:32 PM

So I have a list of buttons which then is shuffled to create shuffleButtons. But I want to compare the buttonOne to the actual 1 in my buttonList to it and see what place it is the shuffledButtons. Then I want it to change to black. Or for the third button I want to compare it to number 3 in my buttonList and change it to black. How do you compare the order of the numbers in the shuffledButtons to the buttonOne in my xaml?


List<int> buttonList = new List<int>(3);
            buttonList.Add(1);
            buttonList.Add(2);
            buttonList.Add(3);
            var shuffledButtons = buttonList.OrderBy(a => Guid.NewGuid());
          
               

            foreach (int i in shuffledButtons)
            {
             buttonOne.Background = Brushes.Black;
                    buttonTwo.Background = Brushes.Black;
                    buttonThree.Background = Brushes.Black;
            }


                


Is This A Good Question/Topic? 0
  • +

Replies To: Timer and Color Reset

#2 codingnoob13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 02-October 13

Re: Timer and Color Reset

Posted 15 December 2013 - 02:28 PM

After this code is performed I want the buttons to go back to being their original colors but when I say buttonOne.Background = brushed.blue; it never changes to black in the first place. I'm not sure if there is a way to wait a few seconds before enacting the code or placing it somewhere else?

void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
         
           Application.Current.Dispatcher.Invoke(() =>
                {

            Random ran = new Random();
            int number = ran.Next(1, 4);
            
                {
               for (int i = 0; i < 3; i++)
               {
                   if (number == 1)
                   {
                       buttonOne.Background = Brushes.Black;
                   }
                   if (number == 2)
                   {
                       buttonTwo.Background = Brushes.Black;
                   }
                   if (number == 3)
                   {
                       buttonThree.Background = Brushes.Black;
                   }
               }
               
       });

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: Timer and Color Reset

Posted 15 December 2013 - 09:53 PM

You're treating this like it is a WinForms project. This code is bad in many ways for WPF.
The buttons should not have names and even if they did they shouldn't be meaningless 'button1'.
But that's trivial compared to you changing their colors in the C# code behind. That's horrible practice for WPF. You should bind the color color to a property. When the property changes the GUI will update.

Please stop working on this until you do some reading on WPF binding and Dependency Properties.

There is an article on WPF for the WinForms coder linked in my signature block.

This post has been edited by tlhIn`toq: 15 December 2013 - 09:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: Timer and Color Reset

Posted 15 December 2013 - 10:05 PM

Dont open multiple threads for the same thing. The volunteers here have lives too. Give them time to respond.putting the question in both C# and WPF just makes it harder on everyone.

This will get merged with
http://www.dreaminco...1&#entry1947544

This post has been edited by tlhIn`toq: 15 December 2013 - 10:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1