vendredi 27 juillet 2018

Random Brush Generator - How to get a new color every time? [duplicate]

This question already has an answer here:

I have a foreach loop, calling the below method. When I step through my code, it is generating a random color. I don't care about the color, just need visual seperation. When I just run the program and don't step through the code, everything is the same color.

I know that a while back I did read a post where someone had the same issue, if you find it please direct me to it. Thanks!

foreach (var opt in OptionList.GroupBy(p => p.PropertyDescription))
    {
      var rndColor = Get_Color();

      foreach (var option in opt)
      {
        option.Foreground = rndColor;
      }
    }

private Brush Get_Color()
{
  Brush retVal = Brushes.Black;

  Random r = new Random();
  int red = r.Next(0, 200);
  int green = r.Next(0, 200);
  int blue = r.Next(0, 200);
  SolidColorBrush brush = new SolidColorBrush(Color.FromRgb((byte)red, 
  (byte)green, (byte)blue));
  retVal = brush;

  return retVal;
}




Aucun commentaire:

Enregistrer un commentaire