Question Problem in creating numeric textbox (.Net 4.0).

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
Hi,
I'm trying to create my own numeric textbox that will handle decimal as well as whole number values. The concept is, unlike the regular textbox, this one will accept a value of 'Double' data type and show it with proper formatting e.g. Leading Digits, Digit Grouping etc. When the textbox value is called, it returns the actual 'Double' data type value. The entire thing is done through the 'Text' property; wherein the regular 'Text' property is shadowed and one with 'Double' data type is used.
The problem is, in the designer properties window, the Text property always remains readonly and its value says 'Object reference not set to an instance of an object'. What's my mistake?
My code follows:

VB.NET:
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Runtime.InteropServices

Public Class NumericTextBox
    Inherits TextBox
    Private _IsDecimal As Boolean

    <System.Diagnostics.DebuggerNonUserCode()> _
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        Me.TextAlign = HorizontalAlignment.Right
    End Sub

    <DefaultValue(False)>
    <ComVisible(True)> _
    Public Property IsDecimal As Boolean
        Get
            Return _IsDecimal
        End Get
        Set(ByVal value As Boolean)
            _IsDecimal = value
        End Set
    End Property

    <DefaultValue(0)>
    <Description("The text associated with the control.")> _
    Public Shadows Property Text As Double
        Get
            Dim ReturnValue As Double
            Double.TryParse(MyBase.Text, ReturnValue)
            Return ReturnValue
        End Get
        Set(ByVal value As Double)
            If Not IsDecimal Then
                MyBase.Text = FormatNumber(value, 0, TriState.True, TriState.False, TriState.True)
            Else
                MyBase.Text = FormatNumber(value, 2, TriState.True, TriState.False, TriState.True)
            End If
        End Set
    End Property

    <DefaultValue(GetType(HorizontalAlignment), "Right")> _
    Public Shadows Property TextAlign As HorizontalAlignment
        Get
            Return MyBase.TextAlign
        End Get
        Set(ByVal value As HorizontalAlignment)
            MyBase.TextAlign = value
        End Set
    End Property

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        MyBase.OnKeyPress(e)

        If Not IsDecimal Then
            If Not Asc(e.KeyChar) = 8 And "0123456789".IndexOf(e.KeyChar) = -1 Then
                e.Handled = True
                Exit Sub
            End If
        Else
            If Not Asc(e.KeyChar) = 8 And "0123456789.".IndexOf(e.KeyChar) = -1 Then
                e.Handled = True
                Exit Sub
            End If
        End If
    End Sub

    Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
        MyBase.OnValidating(e)

        If Trim(MyBase.Text) <> "" And Not IsNumeric(MyBase.Text) Then
            MsgBox("Enter a valid numeric value.", MsgBoxStyle.Exclamation, "Invalid Data")
            e.Cancel = True
        Else
            If Not IsDecimal Then
                MyBase.Text = FormatNumber(Text, 0, TriState.True, TriState.False, TriState.True)
            Else
                MyBase.Text = FormatNumber(Text, 2, TriState.True, TriState.False, TriState.True)
            End If
        End If
    End Sub
End Class
 
I have several recommendations for you:

1. Use Decimal rather than Double.
2. Don't ever change the functionality of the Text property for a control. Override (not shadow) the Text property solely for the purpose of hiding it from the designer and Intellisense.
3. Add a Value property for the numeric value, just as the NumericUpDown does.

Here's one I prepared earlier:

Numeric Text Box
 
Here's one I prepared earlier:

Numeric Text Box

Sorry for the delay.
The optimum solution with dynamic features. String formatting of the value is something that makes it special from other numeric text boxes. Seems you worked hard for it.
Gratitude! :)
 
Back
Top