User control based on System.Web.UI.WebControls.Label (VB)
<%@ Page language="VB" %> <%@ Register TagPrefix="Control" Namespace="Control" Assembly="Control" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <title>Extending Existing Web Controls</title> </head> <body> <form id="Form1" method="post" runat="server"> <Control:RainbowLabel text="This is a rainbow colored test string" runat="server" /><br/> <Control:RainbowLabel EnableRainbowMode="false" text="This is a test string" runat="server" /> </form> </body> </html> File: Control.vb Imports System Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.ComponentModel Namespace Control 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