Viewing source for recipe0302vb.vb

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Namespace AspNetCookbook

    Public Class RainbowLabel
        Inherits System.Web.UI.WebControls.Label

        Public Property EnableRainbowMode() As Boolean
            Get
                If ViewState("EnableRainbowMode") Is Nothing Then
                    Return True
                Else
                    Return Boolean.Parse(CStr(ViewState("EnableRainbowMode")))
                End If
            End Get
            Set(ByVal Value As Boolean)
                ViewState("EnableRainbowMode") = Value
            End Set
        End Property

        Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
            If EnableRainbowMode Then
                output.Write(ColorizeString([Text]))
            Else
                output.Write([Text])
            End If
        End Sub 'Render

        Private Function ColorizeString(ByVal input As String) As String
            Dim output As New System.Text.StringBuilder(input.Length)
            Dim rand As Random = New Random(DateTime.Now.Millisecond)

            Dim i As Integer
            For i = 0 To input.Length - 1
                Dim red As Integer = rand.Next(0, 255)
                Dim green As Integer = rand.Next(0, 255)
                Dim blue As Integer = rand.Next(0, 255)

                output.Append("<font color=""#")
                output.Append(Convert.ToString(red, 16))
                output.Append(Convert.ToString(green, 16))
                output.Append(Convert.ToString(blue, 16))
                output.Append(""">")
                output.Append(input.Substring(i, 1))
                output.Append("</font>")
            Next i

            Return output.ToString()
        End Function 'ColorizeString
    End Class 'RainbowLabel
End Namespace