0

I am building a UWF program and I want to make it so every x seconds 3 random squares change color.

There are around 40 squares in total and each of them are named rec1 - rec42. So my thinking was to randomly pick a square by combining two strings, "rec" and a random int. But now I have to set a field from a string is that even possible? Am I even understanding/approaching this right?

This is my current method

void animatedGraphics_Tick(object sender, object e) {
        //List of color
        String[] possibleColor = { "#FF443806", "#FF332E04", "#FF130F03" };

        Random rnd = new Random();
        //Loop for three square
        for (int i = 0; i < 3; i++)
        {
            //generate pick squares to change
            string square = ("rec" + rnd.Next(1, 43));

            //Something like square.fill = possibleColor[rnd.Next(0,3)];
        }
    }

Thanks

edit: This is what I ended up using

        void animatedGraphics_Tick(object sender, object e)
    {
        //List of color converted to from ARGB values
        Color[] possibleColor = { Color.FromArgb(255, 65, 54 ,9), 
            Color.FromArgb(255, 19, 15, 3), Color.FromArgb(255, 51, 46, 3) };

        Random rnd = new Random();
        //Loop for three square
        for (int i = 0; i < 3; i++)
        {
            //Use FindName to locate the shape 
            Rectangle square = (Rectangle) this.FindName("rec" + rnd.Next(1,42));

            //Change the shape color
            square.Fill = new SolidColorBrush(possibleColor[rnd.Next(0,3)]);
        }
    }
4
  • What is the exact type of the squares? Commented Jan 18, 2021 at 2:21
  • XAML rectangles class ===> <Rectangle x:Name="rec1" Height="16" Width="39" Fill="#FF413609" Margin="2"/> Commented Jan 18, 2021 at 2:23
  • Have you looked at this post? stackoverflow.com/questions/5511484/wpf-rectangle-color-binding Commented Jan 18, 2021 at 2:27
  • I would recommend to put your squares in a list or array of some kind and than acces a random index rather than the object by name this will make your code better servicable Commented Jan 18, 2021 at 7:55

1 Answer 1

1

Probably not the best way to do it but you can get the control by his name calling this.FindName()
Then the Fill method takes a Brush as a parameter, you can use the SolidBrushColor.
Finally to convert from hexa to a Color you can use ColorConverter.ConvertFromString()

Color color = (Color)ColorConverter.ConvertFromString(possibleColor[rnd.Next(0,3)]);  
SolidColorBrush myBrush = new SolidColorBrush(color);  
Rectangle myRectangle = (Rectangle) this.FindName(square);  
myRectangle.Fill(myBrush);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.