ColorComboBox CodeSample - Populating with SystemColors

From Visual WebGui Wiki

Jump to: navigation, search


Overview

This sample is taken from the Catalog sample application, and you can find the GetConstraints() function within RandomData.cs, and the population by using DataSource within the folder

 Categories\Behaviours\ListDataBindingBehaviour.cs

The sample uses SystemColors class and Reflection to build a list of colors to show in the ColorComboBox.

VB.NET code

    Private Sub PopulateCombo()
        ColorComboBox2.DisplayMember = "Name"
        ColorComboBox2.ValueMember = "Name"
        ColorComboBox2.DataSource = GetConstants(GetType(SystemColors))
    End Sub
 
    Private Function GetConstants(ByVal enumType As Type) As Color()
        Dim attributes1 As MethodAttributes = MethodAttributes.[Static] Or MethodAttributes.[Public]
        Dim infoArray1 As PropertyInfo() = enumType.GetProperties()
        Dim list1 As New ArrayList()
        For num1 As Integer = 0 To infoArray1.Length - 1
            Dim info1 As PropertyInfo = infoArray1(num1)
            If info1.PropertyType Is GetType(Color) Then
                Dim info2 As MethodInfo = info1.GetGetMethod()
                If (info2 IsNot Nothing) AndAlso ((info2.Attributes And attributes1) = attributes1) Then
                    Dim objArray1 As Object() = Nothing
                    list1.Add(info1.GetValue(Nothing, objArray1))
                End If
            End If
        Next
        Return DirectCast(list1.ToArray(GetType(Color)), Color())
    End Function

C# Code

Personal tools