Extending the ASP.NET TextField User Control - Developing your own flexible NumericTextbox control: properties to retrieve values
(Page 4 of 4 )
We may need to work with a few more properties apart from the Text property. A few applications need the value to be available with or without formatting. The following are the properties that support all of these scenarios:
<EditorBrowsable(EditorBrowsableState.Always), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _
Bindable(True), Browsable(True), Category("Common")> _
Public Property Text() As String
Get
Return UnFormattedValue(Me.utxtControl.Text)
End Get
Set(ByVal Value As String)
Me.utxtControl.Text = FormattedValue(Value)
End Set
End Property
Private Function UnFormattedValue(ByVal strValue As String) As String
strValue = strValue.Replace("$"c, String.Empty).Replace(","c, String.Empty)
Return strValue
End Function
Private Function FormattedValue(ByVal strValue As String) As String
If Me.IncludeThousandsSeparator Then
If Me.DecimalPlaces > 0 Then
Dim strDecFormat As String = "".PadLeft(Me.DecimalPlaces, "0"c)
Return String.Format("{0:#,##0." & strDecFormat & "}", CType(UnFormattedValue(strValue), Decimal))
Else
Return String.Format("{0:#,##0}", CType(UnFormattedValue(strValue), Decimal))
End If
Else
If Me.DecimalPlaces > 0 Then
Dim strDecFormat As String = "".PadLeft(Me.DecimalPlaces, "0"c)
Return String.Format("{0:#0." & strDecFormat & "}", CType(UnFormattedValue(strValue), Decimal))
Else
Return strValue
End If
End If
End Function
Developing your own flexible NumericTextbox control: new properties added
The following are the new properties added to the control (apart from the properties added from previous uctTextField control):
<Category("Common")> _
Public Property IncludeThousandsSeparator() As Boolean
Get
Return m_blnIncludeThousandsSeparator
End Get
Set(ByVal value As Boolean)
m_blnIncludeThousandsSeparator = value
ApplyRegExpValString()
End Set
End Property
<Category("Common")> _
Public Property DecimalPlaces() As Integer
Get
Return m_intDecimalPlaces
End Get
Set(ByVal value As Integer)
If value < 0 Or value > 5 Then Throw New ApplicationException("Invalid Decimal specification")
If DecimalPlaces <> value Then
m_intDecimalPlaces = value
ApplyRegExpValString()
End If
End Set
End Property
<Category("Common")> _
Public Property MaxLength() As Integer
Get
Return Me.utxtControl.MaxLength
End Get
Set(ByVal value As Integer)
Me.utxtControl.MaxLength = value
End Set
End Property
<Category("Common")> _
Public Property MaxValue() As Decimal
Get
Return m_decMaxValue
End Get
Set(ByVal value As Decimal)
m_decMaxValue = value
End Set
End Property
<Category("Common")> _
Public Property MinValue() As Decimal
Get
Return m_decMinValue
End Get
Set(ByVal value As Decimal)
m_decMinValue = value
End Set
End Property
I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |