0

I want to change random buttons. But question is that i want to do it with random numbers of change colors.

Now i am changing it like this but i want to dynamic change numbers of changing colors.

private void ChangeColor()
{
  Random rand = new Random();
  Color[] colors = new Color[]
      {
          Colors.Red,
          Colors.Blue,
          Colors.Green,
      };


  But0_1.Background = new SolidColorBrush(colors[rand.Next(0, 3)]);
  But0_1.Background = new SolidColorBrush(colors[rand.Next(0, 3)]);
  But0_2.Background = new SolidColorBrush(colors[rand.Next(0, 3)]);
  But0_3.Background = new SolidColorBrush(colors[rand.Next(0, 3)]);
  But0_4.Background = new SolidColorBrush(colors[rand.Next(0, 3)]);
  But0_5.Background = new SolidColorBrush(colors[rand.Next(0, 3)]);
  But0_6.Background = new SolidColorBrush(colors[rand.Next(0, 3)]);
  But0_7.Background = new SolidColorBrush(colors[rand.Next(0, 3)]);
}
moondaisy
  • 4,303
  • 6
  • 41
  • 70

2 Answers2

1

You could do something like :

    private Random _random;

    public MainWindow()
    {
        InitializeComponent();
        _random = new Random();
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        var buttons = GetButtonsToChange();

        foreach (var button in buttons)
        {
            if (button != null)
            {
                button.Background = CreateColor();
            }
        }
    }

    private Button[] GetButtonsToChange()
    {
        var amountOfButtons = RandBetween(0, 5);// <== 0 for none 5 because i only have 4 buttons and RandBetween is less than
        var listOfIndexs = new List<ButtonNumbering>();
        var listOfButtons = new List<Button>();
        for (int i = 1; i <= amountOfButtons; i++)
        {
            var buttonItem = new ButtonNumbering(i,RandBetween(1,4));
            var alreadyHasButton = listOfIndexs.Select(x => x.Number).Contains(buttonItem.Number);
            if (alreadyHasButton)
            {
                while (alreadyHasButton)
                {
                    var newRandomNumber = RandBetween(1, 5);
                    buttonItem.Number = RandBetween();
                    alreadyHasButton = listOfIndexs.Select(x => x.Number).Contains(buttonItem.Number);
                }
            }
            listOfIndexs.Add(buttonItem);
            var buttonName = $"Button{buttonItem.Number}";//<= the names of the buttons are Button1 Button2 Button3 Button4
            var button = (Button)FindName(buttonName);
             listOfButtons.Add(button);
        }

        return listOfButtons.ToArray();
    }

    private SolidColorBrush CreateColor(int bottom=0, int top=255)
    {
        var red = RandBetween(bottom,top);
        var green = RandBetween(bottom,top);
        var blue = RandBetween(bottom,top);

        return new SolidColorBrush(Color.FromRgb(red,green,blue));
    }

    private byte RandBetween(int bottom=0, int top=255)
    {
        var randomNumber = Convert.ToByte(_random.Next(bottom, top));
        return randomNumber;
    }

I have updated the answer as it was creating the same color for all of them.

3xGuy
  • 2,235
  • 2
  • 29
  • 51
  • Thanks a much. But which way i can make list of This Buttons and choose the quantity that will be changed(not all but for example random 10 from 40). – Michał Łahchim Apr 28 '18 at 08:31
  • Thanks for your answer- but this is not what i want. I want to random choose buttons which will be changed(not a colors). I have for example 10 buttons, and on every click for example 3 random buttons will change color. Rest 7 have the same. On next click- next 3 random buttons should be changed. – Michał Łahchim Apr 30 '18 at 11:35
  • Oh, ok, so let me get this strait. you want it to randomly choose buttons to change colors? – 3xGuy Apr 30 '18 at 11:40
  • I have 48 buttons, and on every click i want to draw 20 from 48 buttons which will change color(random color- red blue green). – Michał Łahchim Apr 30 '18 at 11:46
  • I have updated answer, look at notes in answer to make sure you have correct amount of buttons. – 3xGuy Apr 30 '18 at 12:07
0

I can't comment yet, but the RandomInt() method provided by 3xGuy is always going to produce a random color of gray because all 3 bytes will be the same. It's essentially the same problem described here: Random number in a loop

Make the Random instance local to CreateColor and remove the RandomInt() method:

private static SolidColorBrush CreateColor()
{
    var random = new Random();

    var r = Convert.ToByte(random.Next(0, 255));
    var g = Convert.ToByte(random.Next(0, 255));
    var b = Convert.ToByte(random.Next(0, 255));

    return new SolidColorBrush(Color.FromRgb(r, g, b));
}
Chaos
  • 250
  • 2
  • 10
  • Thanks a much. But which way i can make list of This Buttons and choose the quantity that will be changed(not all but for example random 10 from 40). – Michał Łahchim Apr 28 '18 at 08:30