Problem in creating numeric textbox (.Net 4.0 - Winforms).

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • priyamtheone
    New Member
    • Sep 2007
    • 88

    Problem in creating numeric textbox (.Net 4.0 - Winforms).

    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:

    Code:
    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
Working...