Hi,
I've just begun with C# and I've encountered a problem...
I've got 25 buttons on my windows form, they all have black background... My objective is to randomly assign them one of 2 colors (black, white), when my application starts. How can I solve my problem? Could you help me?
Thanks,
Gregory
I've just begun with C# and I've encountered a problem...
I've got 25 buttons on my windows form, they all have black background... My objective is to randomly assign them one of 2 colors (black, white), when my application starts. How can I solve my problem? Could you help me?
Thanks,
Gregory
joakim wireenPosted Apr 12, 2009, 11:13 AM
If you want to implend more colors in the future you could precreate some in an color array and randomly pick them one out ;)
private void button1_Click(object sender, EventArgs e)
{
Random rand = new Random();
Color[] c = { Color.SeaShell, Color.SpringGreen, Color.Teal, Color.Tomato, Color.WhiteSmoke };
this.button1.BackColor = c[rand.Next(0, c.Length)];
}
Or you could randomly generate colors
private void button1_Click(object sender, EventArgs e){
Random randomizer = new Random(); int r = randomizer.Next(0,255), g = randomizer.Next(0,255), b = randomizer.Next(0,255); this.BackColor = Color.FromArgb(r, g, b);}
Mahesh ChandPosted Apr 11, 2009, 10:45 AM
You have only 2 colors. I doesn't really sound random ;)
Anyway, you can generate a random number between 1 and 2. If number is 1, assign white color and if the number is 2, assign black color. Search this site for ranom number to see how to generate a random number.