Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleGenerate random colors in VB .NET
DescriptionThis example shows how to generate random colors in VB .NET.
Keywordscolor, random color, QBColor, FromArgb, VB.NET
CategoriesGraphics, VB.NET
 
Module RandomColors contains two functions that return random colors. Function RandomQBColor selects a random number between 0 and 15 and looks up the corresponding QBColor value. It adds an alpha component of &HFF000000 to the result and uses the Color.FromArgb method to convert the numeric value into a Color.

Function RandomRGBColor randomly generates random red, green, and blue components for a color.

 
Module RandomColors
    Private m_Rnd As New Random

    ' Return a random QB color.
    Public Function RandomQBColor() As Color
        Dim color_num As Integer = m_Rnd.Next(0, 15)
        Return Color.FromArgb(QBColor(color_num) + _
            &HFF000000)
    End Function

    ' Return a random RGB color.
    Public Function RandomRGBColor() As Color
        Return Color.FromArgb(255, _
            m_Rnd.Next(0, 255), _
            m_Rnd.Next(0, 255), _
            m_Rnd.Next(0, 255))
    End Function
End Module
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated