从7个选项中生成随机颜色

时间:2014-12-27 14:48:16

标签: vb.net windows-phone-8

我正在尝试从7个选项生成随机颜色。我发现的所有堆栈溢出帖/教程都是随机颜色。这是颜色列表:

Red = New SolidColorBrush(Color.FromArgb(100, 255, 0, 0))
White = New SolidColorBrush(Color.FromArgb(100, 255, 255, 255))
Blue = New SolidColorBrush(Color.FromArgb(100, 0, 0, 255))
Yellow = New SolidColorBrush(Color.FromArgb(100, 244, 255, 16))
Green = New SolidColorBrush(Color.FromArgb(100, 0, 255, 0))
pink = New SolidColorBrush(Color.FromArgb(100, 255, 16, 22))
Brown = New SolidColorBrush(Color.FromArgb(100, 120, 37, 37))

我想将它们随机分配到Label1.foreground

Label1.Foreground = // I got Stuck at This -,-

我试着给我们一个随机数生成器:

Dim randomColour As New Random

但我不知道怎么做......请帮助我....

2 个答案:

答案 0 :(得分:1)

您可能想要使用SolidBrush

' Create a List
Dim colorList As New List(Of SolidBrush)

' Add colors to it
colorList.Add(New SolidBrush(Color.FromArgb(100, 255, 0, 0)))
colorList.Add(New SolidBrush(Color.FromArgb(100, 255, 255, 255)))
...

' Create a random instance
Dim rnd = new Random()

' Get a random item from the list between 0 and list count
Dim randomColour = colorList(rnd.Next(0, colorList.Count))

' Assign the color to the label
Me.Label1.ForeColor = randomColour.Color

答案 1 :(得分:0)

您也可以在随机点击颜色变化时使用按钮

Public Class Form1

Dim rnd As New Random

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.BackColor = Color.FromArgb(255, rnd.Next(255), rnd.Next(255), rnd.Next(255))
End Sub

End Class