如何使我的lblAnzeige
颜色为红色,白色,绿色,黄色和蓝色? (所以当我点击btnGelb
时,文本lblAnzeige
会改变其中五种颜色之一的颜色)在我的代码中,它改变了一种rgb颜色,但没有改变为正确的5种颜色:(
private void btnGelb_Click(object sender, EventArgs e)
{
int summe = 0, z;
lblAnzeige.Text = " ";
while (summe <= 0)
{
z = r.Next(1, 6);
summe = summe + z;
}
lblAnzeige.Text += colors[summe - 1] + "\n";
lblAnzeige.ForeColor = Color.FromArgb(Farbe.Next(256), Farbe.Next(256), Farbe.Next(256));
}
答案 0 :(得分:3)
您可以将颜色存储在列表或数组中,然后从列表中随机选择一个。
static readonly IList<Color> myColors =
new[] { Color.Red, Color.Blue, Color.Green, Color.White, Color.Yellow };
private void btnGelb_Click(object sender, EventArgs e)
{
int summe = 0, z;
lblAnzeige.Text = " ";
while (summe <= 0)
{
z = r.Next(1, 6);
summe = summe + z;
}
lblAnzeige.Text += colors[summe - 1] + "\n";
lblAnzeige.ForeColor = myColors[Farbe.Next(myColors.Count)];
}
答案 1 :(得分:2)
Private Function RandomColor() As System.Drawing.Color
Dim MyAlpha As Integer
Dim MyRed As Integer
Dim MyGreen As Integer
Dim MyBlue As Integer
Randomize()
MyAlpha = CInt(Int((254 * Rnd()) + 0))
Randomize()
MyRed = CInt(Int((254 * Rnd()) + 0))
Randomize()
MyGreen = CInt(Int((254 * Rnd()) + 0))
Randomize()
MyBlue = CInt(Int((254 * Rnd()) + 0))
Return Color.FromArgb(MyAlpha, MyRed, MyGreen, MyBlue)
End Function
我已经在一个vb项目中拥有它...我不应该很难将它转换为C#
虽然我更喜欢其他答案,但这可能会以更加迂回的方式发挥作用
private void Random()
{
RandomNumber random = new RandomNumber();
int randomInt = random.RandomInRange(1, 5);
if (randomInt == 1)
{
lblLabel.ForeColor = System.Drawing.Color.Red;
}
else if (randomInt == 2)
{
lblLabel.ForeColor = System.Drawing.Color.Yellow;
}
else if(randomInt ==3)
{
lblLabel.ForeColor = System.Drawing.Color.White;
}
else if (randomInt == 4)
{
lblLabel.ForeColor = System.Drawing.Color.Blue;
}
else if (randomInt == 5)
{
lblLabel.ForeColor = System.Drawing.Color.Green;
}
}
class RandomNumber
{
public int RandomInRange(int l, int u)
{
Random generator = new Random();
return generator.Next(l, u);
}
}
答案 2 :(得分:0)
使用这种简单的字体颜色技术,可以进一步扩展..
Random NewRGBColor = new Random();
LastIssueTxtBox.ForeColor = Color.FromArgb(NewRGBColor.Next(256), NewRGBColor.Next(256), NewRGBColor.Next(256));
斯蒂芬