Franz
Franz

Reputation: 5

Random color text

How can I make my lblAnzeige in the colors red, white, green, yellow and blue? (so when i click on btnGelb the text lblAnzeige changes its color in one of the five ones) In my code it change in one rgb color but not in the right 5 ones :(

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));
}

Upvotes: 0

Views: 2388

Answers (3)

Stephen
Stephen

Reputation: 1

Use this simple techenique for font color and can further expand..

    Random NewRGBColor = new Random();
    LastIssueTxtBox.ForeColor = Color.FromArgb(NewRGBColor.Next(256), NewRGBColor.Next(256), NewRGBColor.Next(256));

Stephen

Upvotes: 0

Brandon
Brandon

Reputation: 945

    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

I already had it in a vb project i have...shouldn't be hard for you to convert it to C#

Although I like the other answer better this could work in a more roundabout way

    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);
    }
}

Upvotes: 2

Tim S.
Tim S.

Reputation: 56536

You could store your colors in a list or array and then randomly pick one from the list.

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)];
}

Upvotes: 3

Related Questions