Pages

Tuesday, November 1, 2011

Allow users to select colors without a dialog window (VB .NET)

VB .NET offers a ColorDialog component that you can load to help users pick colors at runtime. However, you don't need a separate window for this task. The .NET Framework consists of 167 system-defined colors. The Color object exposes the FromKnownColor() function, which allows you to access these colors, by index, using the following syntax:

   Color.FromKnownColor(idx)

For the standard system colors, you can pass this function any value from 1 to 167. Although, you may want to skip the first 28 colors, since they're just your basic control colors, like ActiveBorder and ControlText. The following code uses the FromKnownColor() function to dump the system-defined color names out to a ListBox.

 Dim clr As Color
 Dim enumClr As KnownColor
 For enumClr = 28 To KnownColor.YellowGreen
      clr = Color.FromKnownColor(enumClr)
      ListBox1.Items.Add(clr.ToKnownColor.ToString)
 Next



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.