By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

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

P: 79
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:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Windows.Forms
  2. Imports System.ComponentModel
  3. Imports System.Runtime.InteropServices
  4.  
  5. Public Class NumericTextBox
  6.     Inherits TextBox
  7.     Private _IsDecimal As Boolean
  8.  
  9.     <System.Diagnostics.DebuggerNonUserCode()> _
  10.     Public Sub New()
  11.         MyBase.New()
  12.  
  13.         'This call is required by the Component Designer.
  14.         InitializeComponent()
  15.         Me.TextAlign = HorizontalAlignment.Right
  16.     End Sub
  17.  
  18.     <DefaultValue(False)>
  19.     <ComVisible(True)> _
  20.     Public Property IsDecimal As Boolean
  21.         Get
  22.             Return _IsDecimal
  23.         End Get
  24.         Set(ByVal value As Boolean)
  25.             _IsDecimal = value
  26.         End Set
  27.     End Property
  28.  
  29.     <DefaultValue(0)>
  30.     <Description("The text associated with the control.")> _
  31.     Public Shadows Property Text As Double
  32.         Get
  33.             Dim ReturnValue As Double
  34.             Double.TryParse(MyBase.Text, ReturnValue)
  35.             Return ReturnValue
  36.         End Get
  37.         Set(ByVal value As Double)
  38.             If Not IsDecimal Then
  39.                 MyBase.Text = FormatNumber(value, 0, TriState.True, TriState.False, TriState.True)
  40.             Else
  41.                 MyBase.Text = FormatNumber(value, 2, TriState.True, TriState.False, TriState.True)
  42.             End If
  43.         End Set
  44.     End Property
  45.  
  46.     <DefaultValue(GetType(HorizontalAlignment), "Right")> _
  47.     Public Shadows Property TextAlign As HorizontalAlignment
  48.         Get
  49.             Return MyBase.TextAlign
  50.         End Get
  51.         Set(ByVal value As HorizontalAlignment)
  52.             MyBase.TextAlign = value
  53.         End Set
  54.     End Property
  55.  
  56.     Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
  57.         MyBase.OnKeyPress(e)
  58.  
  59.         If Not IsDecimal Then
  60.             If Not Asc(e.KeyChar) = 8 And "0123456789".IndexOf(e.KeyChar) = -1 Then
  61.                 e.Handled = True
  62.                 Exit Sub
  63.             End If
  64.         Else
  65.             If Not Asc(e.KeyChar) = 8 And "0123456789.".IndexOf(e.KeyChar) = -1 Then
  66.                 e.Handled = True
  67.                 Exit Sub
  68.             End If
  69.         End If
  70.     End Sub
  71.  
  72.     Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
  73.         MyBase.OnValidating(e)
  74.  
  75.         If Trim(MyBase.Text) <> "" And Not IsNumeric(MyBase.Text) Then
  76.             MsgBox("Enter a valid numeric value.", MsgBoxStyle.Exclamation, "Invalid Data")
  77.             e.Cancel = True
  78.         Else
  79.             If Not IsDecimal Then
  80.                 MyBase.Text = FormatNumber(Text, 0, TriState.True, TriState.False, TriState.True)
  81.             Else
  82.                 MyBase.Text = FormatNumber(Text, 2, TriState.True, TriState.False, TriState.True)
  83.             End If
  84.         End If
  85.     End Sub
  86. End Class
May 12 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.