Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am making a Calculator app. I want it's TextBox(where the numbers are entered) to accept numbers and a decimal point only. How do I do that? Can anyone help me? Thanx in advance.
Posted
Comments
[no name] 15-May-14 9:51am    
Yes just about anyone can help you if you try something. There must be thousands of examples of this for you to look at. Have you done any research? Have you written any code?

1 solution

Timing! There is a very similar post here Validation for a text box with alphanumeric[^] which just came in!

Using the solution I just posted there you can try this
C#
private void textBox1_Validating(object sender, CancelEventArgs e)
{
    //Cancel the exit from the textBox if it is not all numeric
    e.Cancel = !Regex.IsMatch(textBox1.Text, @"^\d+$");
}
{Edit - Validating event not required here as only numeric allowed and that will be forced by the KeyPress event below
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    //"handle" the key press if is not numeric or backspace
    //because it is handled here it won't appear in the textBox
    e.Handled = !(Char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '.');
}
 
Share this answer
 
v2
Comments
Anas Tasadduq 15-May-14 9:57am    
I'm a beginner, so may I know what is Regex, KeyChar and IsDigit? And what does e.Handled means? And why one method is "Validating" and one is "KeyPress"?
CHill60 15-May-14 10:16am    
See comment from Nilesh Avhad.
e is the parameter set KeyPressEventArgs that is passed to this Event Handler whenever a key is pressed when textBox1 has focus. The Handled property can be set to true to indicate that no further action is required for this keystroke - i.e. it has already been "handled". If set to true then the keyboard character pressed will not appear in the text box.
The reason for the two events ... the KeyPress event is called every time a key is pressed when textBox1 has focus. By using this event to determine whether or not we want to ignore the key (i.e. it is not a number) then we tell the user at the very earliest opportunity that we are not going to accept that particular key as input.
However, if we want to validate the *entire* set of information they have entered into the textbox then we have to wait until they have finished typing, and leave the textbox. That is when the <cod>Validating event will fire.
I've also just realised that I posted both event handlers when really only the KeyPress handler is required (the other one came from an earlier post - sorry). I will amend my solution
Anas Tasadduq 15-May-14 13:03pm    
Thanks a lot!
Emre Ataseven 15-May-14 11:06am    
You need to read something before asking it imo.
Telstra 15-May-14 10:03am    
Regex means Regular Expression. It is used for validation like in this example @"^\d+$" this expression is validating that user should enter number only.
KeyChar means the ASCII code of key which you pressed on keyboard.
IsDigit means is this a digit or character.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900