|
|
Title | Generate a random QBColor value in VB .NET |
Description | This example shows how to generate a random QBColor value in VB .NET. The QBColor function returns an assortment of 16 colors with indexes between 0 and 15. The values are returned as long integers, however. This program generates one of these values and then converts it into a Color. |
Keywords | VB .NET, QBColor, random, random color |
Categories | Graphics |
|
|
The QBColor function returns an assortment of 16 colors with indexes between 0 and 15. The values are returned as long integers, however. The program geneates one of these colors randomly, and then breaks it into its red, green, and blue components. It passes those values to the Color structure's FromArgb method to generate a Color.
|
|
' Return a QBColor value converted into a Color.
Private Function RandomQBColor() As Color
Static color_generator As New Random
Dim qb_color As Long = QBColor(color_generator.Next(0, _
16))
Dim b As Integer = CInt(Int(qb_color / 65536))
Dim g As Integer = CInt(Int((qb_color - b * 65536) / _
256))
Dim r As Integer = CInt(Int(qb_color - b * 65536 - g * _
256))
Return Color.FromArgb(255, r, g, b)
End Function
|
|
The example program uses this function to draw 100 random ellipses whenever the form receives a Resize or Paint event.
|
|
|
|
|
|