<%@Control Language="VB" Debug="True" %>

<asp:TextBox id="txtMaskEdit" Columns="25" runat="server" />

<%-------------------------------------------------------------%>
<script runat="server">

<%-------------- Private Internal Variable -------------------%>
Private _font As String = "Courier New"

<%-------------- Public Property Variables -------------------%>
Public Mask As String
Public FontSize As Integer

<%-------------- Page Load Event Handler ---------------------%>
Sub Page_Load()

  ' add style attributes to Textbox
  txtMaskEdit.Style("font-family") = _font
  txtMaskEdit.Style("font-size") = FontSize & "pt"

  ' create mask for display as Textbox background
  Dim sQuery As String = Mask
  sQuery = sQuery.Replace("a", "_")
  sQuery = sQuery.Replace("A", "_")
  sQuery = sQuery.Replace("l", "_")
  sQuery = sQuery.Replace("L", "_")
  sQuery = sQuery.Replace("n", "_")
  sQuery = sQuery.Replace("?", "_")

  ' encode it for query string to pass to page
  ' mask-image.aspx that generates the image
  sQuery = Server.UrlEncode(sQuery)
  _font = Server.UrlEncode(_font)

  ' create and add background style attribute
  txtMaskEdit.Style("background-image") = "url(mask-image.aspx?mask=" _
    & sQuery & "&font=" & _font & "&size=" & FontSize & "&cols=" _
    & txtMaskEdit.Columns.ToString() & ")"

  ' create string to use as Tooltip for control
  Dim sTip As String = Mask
  sTip = sTip.Replace("a", "[a]")
  sTip = sTip.Replace("A", "[A]")
  sTip = sTip.Replace("l", "[l]")
  sTip = sTip.Replace("L", "[L]")
  sTip = sTip.Replace("n", "[n]")
  sTip = sTip.Replace("?", "[?]")
  txtMaskEdit.ToolTip = "Mask: " & sTip & vbCrlf & " where:" _
    & vbCrlf & "[a] = any alphanumeric character (0-9, A-Z, a-z)" _
    & vbCrlf & "[A] = an uppercase alphanumeric character (0-9, A-Z)" _
    & vbCrlf & "[l] = any letter character (A-Z, a-z)" _
    & vbCrlf & "[L] = an uppercase letter character (A-Z)" _
    & vbCrlf & "[n] = any numeric character (0-9)" _
    & vbCrlf & "[?] = any character"

  ' add client-side event handler attributes
  txtMaskEdit.Attributes.Add("onkeydown", "return doKeyDown(event, this, '" & Mask & "')")
  txtMaskEdit.Attributes.Add("onkeypress", "return doKeyPress(event, this, '" & Mask & "')")
  txtMaskEdit.Attributes.Add("onkeyup", "return doKeyUp(event, this, '" & Mask & "')")
  txtMaskEdit.Attributes.Add("onfocus", "return doFocus(event, this, '" & Mask & "')")

  ' see if previous instance of this control has already
  ' added the required JavaScript code reference to the page
  If Not Page.IsClientScriptBlockRegistered("AHHMaskEdit") Then
    Dim sPath As String = "/aspnet_client/custom/"
    Dim sScript As String = "<script language='javascript' " _
      & "src='" & sPath & "maskedit.js'><" & "/script>"
    ' add this JavaScript code to the page
    Page.RegisterClientScriptBlock("AHHMaskEdit", sScript)
  End If

End Sub

</script>
<%-------------------------------------------------------------%>