Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
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
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
Posted
Comments
Sergey Alexandrovich Kryukov 12-May-12 22:45pm    
No, its not "its value says", this is exception. In what line. Did you run it under debugger?
--SA

1 solution

Try the following
Shadows Text property hides the base class Text property. Hence usage of Mybase.text in my opinion is not successful. So, make the method overrides and use text as string. Have another property to report double value. In the designer hide the text value and report the double. Report the success or otherwise of it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900