473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

88 64KB
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
0 1092

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

Similar topics

0
by: Chung Ta | last post by:
Hi all, I am having problem creating web application using Visual Studio .Net. I encountered the following error: Unable to create Web project 'WebApp1'. Unable to validate that the file path...
2
by: umilmi81 | last post by:
I am having a problem creating an ASP.NET application in Visual Studio.NET 2003, installed on Windows XP Professional I get a message stating that I do not have ASP.NET 1.1 installed, so I can not...
0
by: Raphaël Désalbres | last post by:
Hello, everybody, I'm having a problem creating a control...All I want to do is a simple poll system, so I need to input (trough a ListitemCollection) the values from the user. The problem is...
0
by: Phillip Ian | last post by:
I have a textbox that I'm trying to limit to numeric entry (damn you Microsoft for not including it in .NET!) I've found sample code and come up with this as a solution: <asp:TextBox...
11
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
1
by: Dinesh | last post by:
Hi I want an approach(Procedures - steps - logic) on creating a textbox control. In detail, how should i go for the Painting , handling the key inputs for that control.... I dont want to...
0
by: sjickells | last post by:
Hi I am having a problem using asp:TextBox's in a transparent table. I have a background image on the page and a table in the middle of the page. I have set the background colour of the table...
2
by: FFrozTT | last post by:
I am having a problem creating a DLL with an entry point. I've been trying sub Main, DllMain, and I get nothing. When I run dumpbin - exports mydll.dll I see no entry points, also the dll when...
1
by: Bill Woodruff | last post by:
Visual Studio 2005, .NET FrameWork 2.0, C#, WinForms Application Hi, I've read the recent posts by and to 'Thunderbird' (and learned a lot, thanks, from the usual masters Skeet and Paladino,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.