Create a WPF Form to choose a text color

I wanted to choose some nice colors for various kinds of text, so I wrote a simple text color picker using Windows Presentation Foundation (WPF).

The code creates a TextBlock and 4 slider controls: one for Alpha channel (Opacity) and one each for Red, Green and Blue.

The code shows how to add child controls to the WPF Form. (You can also use the Grid Class to position controls)

As you move the sliders, the color of the TextBlock changes and it displays the desired color value in the desired color (foreground and background)

Note that the HandleColorChange needs no parameters: due to the Relaxed Delegates feature of VB9.

Start Visual Studio 2008

Choose File->New Project->Visual Basic->WPF Application

You can use the WPF Forms designer, or you can write your code in a program.

Double click the form designer to bring up the Window1.xaml.vb file. Replace the contents with the code below.

Class Window1

    Dim txtBlock As New TextBlock

    Dim sld(3) As Slider ' Alpha, Red, Green, Blue

    Dim WithEvents chk As New CheckBox

    Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

        Me.Height = 400

        Dim MyGrid As Grid = Me.Content

        Dim cv As New Canvas

        txtBlock.Text = "Some Text to color"

        txtBlock.HorizontalAlignment = Windows.HorizontalAlignment.Center

        cv.Children.Add(txtBlock)

        With chk

            .Content = "Check to change background"

        End With

        Canvas.SetTop(chk, 20)

        cv.Children.Add(chk)

        MyGrid.Children.Add(cv)

        For i = 0 To 3

            sld(i) = New Slider

            With sld(i)

                .AutoToolTipPlacement = Primitives.AutoToolTipPlacement.TopLeft

                .HorizontalAlignment = Windows.HorizontalAlignment.Stretch

                .Minimum = 0

                .Maximum = 255

                .Width = 200

                .Orientation = Controls.Orientation.Horizontal

                .HorizontalAlignment = Windows.HorizontalAlignment.Center

                .SmallChange = 10

                If i = 0 Then ' for Alpha channel

                    .Value = 255

                Else

                    .Value = 0 ' for RGB: black

                End If

            End With

            Canvas.SetTop(sld(i), i * 30 + 60)

            cv.Children.Add(sld(i))

            AddHandler sld(i).ValueChanged, AddressOf Me.HandleColorChange

        Next

    End Sub

    Friend Sub HandleColorChange()

        Dim cColor As New Color

        cColor.A = &HFF

        For i = 0 To 3

            Select Case i

                Case 0

                    cColor.A = sld(i).Value

                Case 1

                    cColor.R = sld(i).Value

                Case 2

          cColor.G = sld(i).Value

                Case 3

                    cColor.B = sld(i).Value

            End Select

        Next

        If chk.IsChecked Then

            Me.txtBlock.Background = New SolidColorBrush(cColor)

        Else

            Me.txtBlock.Foreground = New SolidColorBrush(cColor)

        End If

        Me.txtBlock.Text = String.Format("Some Text to color {0:x} {1:x} {2:x} {3:x}", cColor.A, cColor.R, cColor.G, cColor.B)

    End Sub

End Class

End of code