Reputation:
Their is a label named lbltitle
in my first form to display the title, i had changed its color according to the user logged in. following is the code for choose the color:-
Dim n As Integer = Len(userId) Mod 3
Dim lblcolor As Color
Select Case n
Case 0 : lblcolor = Color.Blue
Case 1 : lblcolor = Color.BlueViolet
Case 2 : lblcolor = Color.Red
End Select
lbltitle.ForeColor = lblcolor
i shuffle the color on the basics of the length of userId.
i had extended the code by using random number instead for 3 in modules operation then my code will be changed as:
Dim random_no As Random = New Random(2)
Dim n As Integer = Len(userId) Mod random_no.Next
Dim lblcolor As Color
Select Case n
Case 0 : lblcolor = Color.Blue
Case 1 : lblcolor = Color.BlueViolet
Case 2 : lblcolor = Color.Red
Case 3 : lblcolor = Color.AliceBlue
Case 4 : lblcolor = Color.DarkGoldenrod
End Select
lbltitle.ForeColor = lblcolor
My Question is that is their any possibility to generate random colors instead for this?
Upvotes: 1
Views: 2785
Reputation: 1117
you can use button when clicked color chaenged in random
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.lbltitle.foreColor = Color.FromArgb(255, rnd.Next(255), rnd.Next(255), rnd.Next(255))
End Sub
End Class
Upvotes: 0
Reputation: 11773
Here are two alternatives that set the ForeColor based on the BackColor. The example cycles through the known colors
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'all KnownColors
Dim allColors As List(Of Color) = (From ac In [Enum].GetValues(GetType(KnownColor))
Select Color.FromKnownColor(DirectCast(ac, KnownColor))).ToList
For Each c As Color In allColors
Label1.BackColor = c 'try every color as BackColor
''alternative 1
' ''ForeColor is inverted BackColor
''Dim R As Integer = Not c.R
''Dim G As Integer = Not c.G
''Dim B As Integer = Not c.B
''Label1.ForeColor = Color.FromArgb(Label1.BackColor.A, R, G, B)
'alternative 2
Dim R As Integer = c.R
Dim G As Integer = c.G
Dim B As Integer = c.B
If (R * R + G * G + B * B) < 128 * 128 Then
Label1.ForeColor = Color.White
Else
Label1.ForeColor = Color.Black
End If
Label1.Refresh()
Threading.Thread.Sleep(50)
Next
End Sub
Upvotes: 0
Reputation: 1326
Replace your select case and random numbers with the following code
Dim rand As New Random
lbltitle.ForeColor = Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256))
Upvotes: 3