Search code examples
vb.netwindows-phone-8

to generate a random colour from 7 options


I'm trying to generate a random colour from 7 options. All of the stack overflow posts / tutorials I've found have been ANY random colour. This is the list of the colour :

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

i want to randomthem to Label1.foreground :

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

I try to us a random number generator:

Dim randomColour As New Random

but I'm Stuck how to do that ... Pls Help Me ....


Solution

  • You may want to use 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