Only controls that accept numeric input in C #

Source: Internet
Author: User
Tags numeric

When you need to get text input in MFC, you often use CEdit or its subclasses. You can control some of the behavior of the edit control by setting its edit controls styles, for example, by setting the Es_number identity to allow the control to accept only numbers (although you can copy-paste non-numeric strings into this control).

In. NET, the control used to get text input is a textbox, but the textbox itself does not contain a method or property that can be invoked directly to set it to accept only numeric input. There are several ways to solve this problem:

Inherits the TextBox and overwrites its CreateParams property, adding a es_number identity to the style member of the property, which is the same as initializing CEdit with the edit control styles in MFC. Can be said to be the most lazy way.

The KeyDown event of the textbox is monitored by itself, which realizes the input verification (but does not guarantee the correctness of the copy-paste input); In fact, setting Es_number does the same thing, and if you want to implement the same functionality as the default in Windows controls, you don't have to listen to the KeyDown event yourself. If you need additional functionality, such as allowing you to enter negative numbers, hexadecimal numbers, and so on, which are not available by default, listening for KeyDown events is an effective way.

A control inherited from a TextBox that is written by a third party; it is the practice of people who have the spirit of "not reinventing the wheel". Since getting digital input should be a common problem, someone must have solved it before, then use someone else's solution.

There are several implementations of this connection on CodeProject alone:

Validating Edit Controls. Includes many versions of TextBox subclasses such as NumericTextBox, Alphanumerictextbox, Datetextbox, and so on. It's worth a visit.

A numeric textbox with a twist.

Numeric Textbox:allow your users to enter Numeric data the easy way.

Use the MaskedTextBox control; This is a subclass of a textbox from the. NET framework, implementing a filtered textbox that customizes the format of the accepted input. Just set its string mask property. If you feel that the Es_number function is not enough, and listen to KeyDown events to do the validation is not elegant, this maskedtextbox is definitely worth considering the choice. For example, to accept numbers from 0 to 999999, simply set the Mask property to "999,999.00" on the line (meaning six-bit optional decimal digits, a decimal number, and two digits that must be entered). There is a simple walkthrough of this control on MSDN.

Use the NumericUpDown control. When you need to get a simple numeric input, the most straightforward way to do this in the. NET world is not to try to wrestle with the textbox, but to change the control to use--numericupdown. This control can not only accept digital input from the keyboard, but also a set of up and down arrows to step in. It contains a number of properties that can be set, such as the bool ThousandsSeparator that displays the delimiter comma, the decimal minimum/decimal maximum property that controls the minimum/maximum value, and so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.