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

<script runat="server">

<%-------------- Private Internal Variables ------------------%>
Private _columns As Integer = 3
Private _increment As Integer = 1
Private _maxvalue As Integer = 99
Private _minvalue As Integer = 0
<%------------------------------------------------------------%>

<%------------------ Public Method ---------------------------%>
Public Function ShowMembers() As String
  Dim sResult As String = "<b>SpinBox User Control</b>" _
    & "</p><b>Properties:</b><br />" _
    & "AutoPostback (Boolean, default False)<br />" _
    & "CssClass (String)<br />" _
    & "Columns (Integer, default 3)<br />" _
    & "Increment (Integer, default 1)<br />" _
    & "MaximumValue (Integer, default 99)<br />" _
    & "MinimumValue (Integer, default 0)<br />" _
    & "Text (String)<br />" _
    & "Value (Integer)<br />"
  Return sResult
End Function
<%------------------------------------------------------------%>

<%-------------- Public Property Variables -------------------%>
Public AutoPostback As Boolean = False
Public CssClass As String = ""
<%------------------------------------------------------------%>

<%------------ Property Accessor Declarations ----------------%>
Public Property Columns As Integer
  Get
    Return _columns
  End Get
  Set
    If (value > 0) And (value < 1000) Then
      _columns = value
    Else
      Throw New Exception("Columns must be between 1 and 999")
    End If
  End Set
End Property

Public Property Increment As Integer
  Get
    Return _increment
  End Get
  Set
    If value > 0 Then
      _increment = value
    Else
      Throw New Exception("Increment must be greater than zero")
    End If
  End Set
End Property

Public Property MaximumValue As Integer
  Get
    Return _maxvalue
  End Get
  Set
    If value > _minvalue Then
      _maxvalue = value
    Else
      Throw New Exception("MaximumValue must be greater " _
                        & "than current MinimumValue")
    End If
  End Set
End Property

Public Property MinimumValue As Integer
  Get
    Return _minvalue
  End Get
  Set
    If value < _maxvalue Then
      _minvalue = value
    Else
      Throw New Exception("MinimumValue must be less " _
                        & "than current MaximumValue")
    End If
  End Set
End Property

Public Property Text As String
  Get
    Return textbox.Text
  End Get
  Set
    Dim iValue As Integer
    Try
      iValue = Int32.Parse(value)
    Catch
      Throw New Exception("Text property must represent " _
                        & "a valid Integer value")
    End Try
    If (value >= _minvalue) And (value <= _maxvalue)
      textbox.Text = value
    Else
      Throw New Exception("Text property must be within " _
            & "the current MinimumValue and MaximumValue")
    End If
  End Set
End Property

Public Property Value As Integer
  Get
    Try
      Return Int32.Parse(textbox.Text)
    Catch
    End Try
  End Get
  Set
    If (value >= _minvalue) And (value <= _maxvalue)
      textbox.Text = value.ToString()
    Else
      Throw New Exception("Value property must be within " _
            & "the current MinimumValue and MaximumValue")
    End If
  End Set
End Property

<%------------------------------------------------------------%>

Sub Page_Load()

  ' control ID prefix for contained controls
  Dim sCID As String = Me.ID & "_"

  ' create true/false string for JavaScript code
  Dim sAutoPostback As String = "false"
  If AutoPostback Then
    sAutoPostback = "true"
  End If

  ' create JavaScript parameter string - used to set
  ' parameters for client-side control event handlers
  Dim sParams As String = "'" & sCID & "textbox', " _
    & _minvalue.ToString() & ", " _
    & _maxvalue.ToString() & ", " _
    & _increment.ToString() & ", " _
    & sAutoPostback

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

  End If

  If CssClass <> "" Then
    textbox.CssClass = CssClass
  End If

  SetColumns()
  SetMaxMinValues()

  ' set client-side event handlers for controls
  imageup.Attributes.Add("onclick", "return incrementValue(" & sParams & ")")
  imagedown.Attributes.Add("onclick", "return decrementValue(" & sParams & ")")
  textbox.Attributes.Add("onblur", "return checkValue(" & sParams & ")")
  textbox.Attributes.Add("onkeydown", "return keyDown(event, " & sParams & ")")

End Sub

' set width of Textbox and position images
Private Sub SetColumns()
  textbox.Columns = _columns
  textbox.Style("width") = Columns * 10
  imageup.Style("left") = textbox.Style("width")
  imagedown.Style("left") = textbox.Style("width")
End Sub

' check if current value of Textbox is within
' current max and min limits, and reset if not
Private Sub SetMaxMinValues()
  Dim iValue As Integer
  Try
    iValue = Int32.Parse(textbox.Text)
  Catch
    iValue = _minvalue
  End Try
  If iValue < _minvalue Then
    iValue = _minvalue
  End If
  If iValue > _maxvalue Then
    iValue = _maxvalue
  End If
  textbox.Text = iValue.ToString()
End Sub

</script>

<%------------- HTML Control Declarations --------------------%>

<span id="spindiv" Style="position:relative" runat="server">
<asp:TextBox Style="top:0;left:0;text-align:right" id="textbox"
     runat="server"/>
<asp:ImageButton id="imageup" Style="position:absolute;top:0"
     ImageUrl="~/images/spin-up.gif" runat="server" />
<asp:ImageButton id="imagedown" Style="position:absolute;top:10"
     ImageUrl="~/images/spin-down.gif" runat="server" />
</span>

<%------------------------------------------------------------%>