2014-12-27 39 views
0

我試圖從7個選項生成隨機顏色。我發現的所有堆棧溢出帖子/教程都是隨機的顏色。這是顏色的列表:我想randomthem到Label1.foreground從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 = // I got Stuck at This -,- 

我試着給我們一個隨機數發生器:

Dim randomColour As New Random 

但我被困如何做到這一點...請幫助我....

+2

把他們所有的陣列中的顏色變化時,你可以使用按鈕,並選擇一個隨機數在0和陣列的大小之間 – Mureinik 2014-12-27 14:50:06

回答

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 
+0

Tha你保存我的生活:D – 2014-12-27 15:14:04

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