Varun Behl
Varun Behl

Reputation: 43

set random color to a textbox from a set of colors in windows phone?

I am using a string array for indexing colors.

string[] fore {"Colors.Yellow","Colors.Red","Colors.Blue","Colors.White","Colors.Green"};
int sIndex = rnd.Next(fore.Length);
textblock.Foreground = new SolidColorBrush(fore[sIndex]);

But it gives an invalid argument error? What to do ?

Upvotes: 1

Views: 501

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98740

Well, since fore[sIndex] is a string, looks like SolidColorBrush doesn't have a constructor takes string as a parameter. But it has a constructor that takes Color as a parameter.

You can change it to Color array instead of string array.

Color[] fore = new[] { Color.Yellow, Color.Red, Color.Blue, Color.White, Color.Green };
int sIndex = rnd.Next(fore.Length);
textblock.Foreground = new SolidColorBrush(fore[sIndex]);

Upvotes: 1

Jeroen1984
Jeroen1984

Reputation: 1686

You are putting in a string in the SolidColorBrush Constructor. I think it needs a Color object. Try make a Color[] instead of a string array:

     Color[] fore=  {Color.Yellow,Color.Red,Color.Blue,Color.White,Color.Green };
     int sIndex = rnd.Next(fore.Length);
     textblock.Foreground = new SolidColorBrush(fore[sIndex]);

Upvotes: 1

Related Questions