Problem in creating numeric textbox (.Net 4.0 - Winforms).
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

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

  1. #1
    Join Date
    Sep 2008
    Posts
    79

    Question 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

  2. #2
    Join Date
    Apr 2012
    Posts
    29

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

    I'd advise against shadowing the Text property to alter its behavior. There may be code in the base class that depends on it being a String. I have also written my own numeric TextBox control in the pass and what I did was expose a numeric property for retrieving its value and left the Text property alone.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

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

    You should create a property for your numeric values. Simpler, why not just use a Masked Edit box?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center