Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on a windows phone 7 application.

Assigning a new background to a button does not work properly when it is executed iteratively.

I have a method which is called when the game is reset. All the buttons' backgrounds are changed to new ones. But this doesnt work properly. Sometimes, the backgrounds are not assigned to new ones, and old ones remain. Can someone help with this problem.

C#
public void PaintButtons()
        {
            p1numlist = GenerateRandom();
            p2numlist = GenerateRandom();
            
                
                p1b1.Background = new SolidColorBrush(a[(int)p1numlist[0]]);
                p1b2.Background = new SolidColorBrush(a[(int)p1numlist[1]]); 
                p1b3.Background = new SolidColorBrush(a[(int)p1numlist[2]]);
                p2b1.Background = new SolidColorBrush(a[(int)p2numlist[0]]);          
                p2b2.Background = new SolidColorBrush(a[(int)p2numlist[1]]);          
                p2b3.Background = new SolidColorBrush(a[(int)p2numlist[2]]);
                
                imageindex = rand.Next(3);
                image1.Source = new BitmapImage(uri[imageindex]);
            
            
        }
Posted
Comments
VigneshPT 29-Oct-11 9:32am    
Forgot to mention Color[] a = { Colors.Red, Colors.Blue, Colors.Yellow };
VigneshPT 29-Oct-11 9:33am    
I've even tried to pass the above method as parameter to the Dispatcher.BeginInvoke() but the same problem persists.
VigneshPT 29-Oct-11 10:16am    
Is there a problem with my question???
VigneshPT 29-Oct-11 12:47pm    

Are you sure that the bug is with the buttons drawing?

Looks like you're generating a random number between 1 and 3, then using that as an index into an array of colors. You have a 33% chance of getting yellow every time. Same with red and blue. So it's not surprising to see that the buttons occasionally get the same colors.

Said another way...the colors are changing, you just don't see when they change from Yellow to Yellow.

What about a different approach? You could have an array of brushes that you just cycle through. Rather than picking random numbers, you could pick a random offset and use %, the modulus operator[^] to help grab the 'new' brush color.

Sorry if I'm not reading your code correctly...if this doesn't appear to be the case, perhaps attach a debugger to your code, set some break points and even some logging to see what the old color and new color were and are.

Hope this helps some.

Cheers.
 
Share this answer
 
Comments
Shmuel Zang 30-Oct-11 4:55am    
My 5. But, see my solution.
VigneshPT 30-Oct-11 7:51am    
I meant that suppose a button has yellow, the other buttons in the same group should have different colors(i.e, blue or red) but sometimes two buttons in the same group have same colors. And sometimes, no matter how many times the method is called, there are no changes in the background of the buttons. I have tried many times.
VigneshPT 30-Oct-11 8:18am    
Thank you TheyCallMeMrJames, but the Random number generation was not a problem. I have checked that using watches in IDE, to understand my problem please have a look at the emulator behaviour.
VigneshPT 30-Oct-11 8:56am    
Maybe I am not able to state my problem very clearly, so please have a look at the complete code in the files

See my solution to your previous question (Problem when loading images to buttons dynamically in Windows Phone application[^]). It looks like the same problem.

 
Share this answer
 
Comments
hzawary 30-Oct-11 8:09am    
This problem is exactly the same "(Problem when loading images to buttons dynamically in Windows Phone application)", only posted again and not resolved still!
Probem is solved. Instead of directly changing the background of the button, using the reference of the background actually solves the problem.

I used the following code to change the backround for each button individually
C#
public void PaintButtons()
        {
            
            p1list = GenerateRandom();
            p2list = GenerateRandom();


            ChangeBackground(p1b1, p1b2, p1b3, p2b1, p2b2, p2b3);
            ***********
            **********
        }
public void ChangeBackground(Button one, Button two, Button three, Button four, Button five, Button six)
        {
            SolidColorBrush s = (SolidColorBrush)one.Background;
            s.Color = a[(int)p1list[0]];
            one.Background = s;
             s = (SolidColorBrush)two.Background;
            s.Color = a[(int)p1list[1]];
            two.Background = s;
             s = (SolidColorBrush)three.Background;
            s.Color = a[(int)p1list[2]];
            three.Background = s;
             s = (SolidColorBrush)four.Background;
            s.Color = a[(int)p2list[0]];
            four.Background = s;
             s = (SolidColorBrush)five.Background;
            s.Color = a[(int)p2list[1]];
            five.Background = s;
             s = (SolidColorBrush)six.Background;
            s.Color = a[(int)p2list[2]];
            six.Background = s;
        }
 
Share this answer
 

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