This article shall describe the construction of a custom control used to display currency values. The control is intended for use in Win Forms application development. The purpose of control is to allow the user to enter numeric values which are subsequently converted to and displayed as currency as is indicated in figure 1 below. The user of the control may drop any number of the currency controls into a form to display numeric values as currency; the control itself is merely an extended textbox.
By placing the code necessary to support the functionality into the format of a custom control, the need for writing the code necessary to support the display of currency for each textbox used is eliminated. Getting Started. The solution contains two projects. The first project is provided to demonstrate use of the control in a Win Forms application.
The second project contains the actual custom control which is called "CurrencyTextBox". The Custom Control Project. The custom control's code contains all of the necessary methods used to restrict user input and to display the values into a textbox as a currency value. The control contains only the default imports as shown in the following:. Following the using statements and the namespace declaration, the class is configured to inherit from the standard textbox control.
By inheriting from the textbox control, all of the functionality of the control is included. After declaring the class, the next section of code is used to declare a private member variable which, through a public property, is used to store the current dollar value. Maintaining the dollar value as an internal variable is necessary to support edits to the underlying value after the value has been converted to a formatted string.
It further allows the user to perform calculations against this value when, for example, the contents of multiple controls are added together. InitializeComponent. OnPaint pe. The next section of code in the control is used to handle the keypress event. The purpose of the keypress event handler is to limit the user to keying in numbers and decimal points. The handler also allows the users to use the backspace button and other control characters when editing the contents of the currency text box.
This code also limits the user to entering in only a single decimal point.
Subscribe to RSS
IsDigit e. IsControl e. Contains ". After the keypress event handler, the next section of code is used to handle the "validated" event for the control.
This handler will attempt to convert the contents of the textbox to a properly formatted currency value. ToDecimal this. In the case of this control, the intent is to convert the value displayed in the textbox back to the actual underlying control value as it stored in control the actual decimal value. If the current dollar value property is zero, the application will clear the textbox so that the user does not end up with the cursor trailing the zero.
If the dollar value property does contain a value, the cursor is positioned at the end of that value. ToString. Clear. The next section of code after the click event handler is the code used to handle the text changed event for the control. In this instance, whenever the value in the control is changed, the dollar value property is updated to hold the latest value.
Text .In some applications we need to show the input text in a TextBox with some mask, like, for example, a currency mask to make it more easy to read and understand. In my case, I needed to put text, and additionally, the currency symbol, once the text was written. To do that, I decided to develop a control that inherits from the original TextBox control and implements some new members to allow the control to validate and show the input text with the currency mask we need.
The TextBox control is one of the basic and most used controls in WinForms applications that allows the user to input text to interact with the application. It has basics behaviors and members that allow us to use it instantly without having to add anything to the control. Based on the TextBox control, we are going to develop a TextBox that can mask our input text with currency format, including the currency symbol that we want. Inheritance is a way to compartmentalize and reuse code by creating objects that have attributes and behaviors that can be based on previously created objects.
Inheritance allows us to work with simple controls like TextBox by taking advantage of their basic behaviors and lets us improve the control by adding new members to control some behaviors that we need to. It's a feature that allows a child class to provide a specific implementation of a method that's already provided by its parent class.
The demo project will show you how to implement the currencyTextBox control in a WinForms project, where you can test anything you want about the control.
Display Currency Values with a Custom Control
In the currencyTextBox class in the JRINCCustomControls project are various custom members that help us achieve the goal of putting the currency-mask to our input text. These are:. It's a read only property that contains our "working text". That means that it contains exactly what the user typed in the TextBox, the text without any mask or formatting. It's a public property that can be assigned and retrieved. It must contain the prefix or currency symbol that we want to precede the input text with.
If empty, no prefix is used on the masked text. It can be assigned through the Properties window at design time. It must contain the separator symbol for thousands that we want our input text to show when masked. If empty, no thousands separator is used on the masked text. It must contain the separator symbol for decimals that we want our input text to show when masked. If empty, no decimal separator is used in the masked text.
It must contain the total places for decimal values that we want our input text to show when masked. Minimum value is 0. It formats the entered text with the desired specifications. Returns a string with the masked text.
This method allows the currencyTextBox to load the formatted text to the Text property. It executes the formatText method, then proceeds to execute the original implementation of the OnLostFocus method. This method allows the currencyTextBox to load the working text to the Text property. In this state, the control is in edit mode please refer to the WorkingText property definition above. After this, it proceeds to execute the original implementation of the OnGotFocus method.
This method brings some validation to ensure that the values entered to the TextBox are only numbers.If in your application have a sign up or sign in functionality, then you need to take input like Name, user ID and password etc. Not only text format it also provides an option like — CheckBox and Radio Button. See below simple example, to write a name in a text field. Here are some of the additional attributes you can use in the HTML input type text field.
The adding an attribute will automatically support and give additional features to a text field. Get the value of a text field is easy.
Using Size attribute in input html tag will indicating how many characters wide the input field should be. The value is of it numeric. Where the value must be a number greater than 0, and the default value is See the below simple example of it. For example address of the customer in the shopping site have to give option multiline.
Another one example of a comment section also has multiline. See below the code for HTML input multiline. Set the limit minimum number of characters long the input shou ld be and still be considered valid.
Add or remove a currency symbol in a text box or expression box
A stop to user input any data, its a Boolean attribute indicating whether input be read-only or not.Keep in touch and stay productive with Teams and Officeeven when you're working remotely.
Learn how to collaborate with Office Tech support scams are an industry-wide issue where scammers trick you into paying for unnecessary technical support services. You can help protect yourself from scammers by verifying that the contact is a Microsoft Agent or Microsoft Employee and that the phone number is an official Microsoft global customer service number.
Did this solve your problem? Yes No. Sorry this didn't help. April 14, Keep in touch and stay productive with Teams and Officeeven when you're working remotely. Site Feedback. Tell us about your experience with our site. JamesJoey Created on December 1, Any way to have an input mask for a Currency field? Thanks, James. This thread is locked.
You can follow the question or vote as helpful, but you cannot reply to this thread. I have the same question Scottgem Replied on December 1, Thanks, James A currency field is automatically formatted for currency and converted after data entry so there is no need for an input mask.
Please post a response to let us know whether our answer helped or not. Thanks for marking this as the answer. How satisfied are you with this reply? Thanks for your feedback, it helps us improve the site.
How satisfied are you with this response?
I tried few codes that I found here and there but it didn't work. Hope someone can help me and give me some detailed information too. Learn more. How to make textbox accept only currency format input? Ask Question.
Asked 4 years, 4 months ago. Active 1 month ago. Viewed 8k times. Tinaira Tinaira 5 5 silver badges 15 15 bronze badges. Active Oldest Votes. You can create a TextChanged event and format the text as you go. Parse textBox1. IsControl e. IsDigit e. IndexOf '. Edgar Mora Edgar Mora 96 5 5 bronze badges. Parse I made decimal.
A Currency-Masked TextBox from TextBox Class
Using the code provided, when I type the numbers are reversed in order as a type. So trivial stuff did so much problems and headaches. MaskedTextBox is really useless control. Thanks for this solution, works like a charm! TryParse TextBox. Focus ; TextBox.
Mohsen K Mohsen K 1 1 silver badge 8 8 bronze badges. Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.If you change the input values and then click the "Reset" button, the form-data will be reset to the default values. You can also use the min and max attributes to add restrictions to dates:. Some smartphones recognize the email type, and add ". The following example displays a numeric input field, where you can enter a value from 0 toin steps of The default value is Default range is 0 to However, you can set restrictions on what numbers are accepted with the minmaxand step attributes:.
Some smartphones recognize the url type, and adds ". Start the Exercise. If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:. Links Link Colors Link Bookmarks. The characters in a password field are masked shown as asterisks or circles. Exercise: In the form below, add an input field for text, with the name "username".
HOW TO. Your message has been sent to W3Schools. W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.
You can add currency symbols only for text box controls that have a decimal or whole number data type, or for expression box controls that are formatted as a decimal or whole number. Adding or removing a currency symbol affects only the displayed value, not the value that is saved in the data source. Double-click the text box control or expression box control whose data you want to format. In the Control Properties dialog box, do one of the following:.
For a text box, ensure that the Data type list displays the appropriate data type, and then click Format. For an expression box, ensure that the Format as list displays the appropriate data type, and then click Format.
In the Data Type Format dialog box, do one of the following:. To add a currency symbol, click Currencyand then in the Currency list, select the type of currency that you want to display. Learn more. Expand your Office skills. Get new features first. Was this information helpful? Yes No. Any other feedback? How can we improve? Send No thanks. Thank you for your feedback! It sounds like it might be helpful to connect you to one of our Office support agents.