Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
4.57/5 (7 votes)
See more:
I need a Text box that can only have integer values.
when user click the ok button, it should check whether the input in text box is an integer value, and if it isn't an integer value then it should show the message to enter only integer value.
and should reset the value to null.


I have done it this way.
C#
private void ok_Click(Object sender, EventAegs e)
{
   if(System.Text.regularExpressions.Regex.IsMatch("[^0-9]",exp_weight.Text))
    {
        ok=true;
        this.Hide();    
    }
    else{
    MessageBox.Show("Please Enter Number Only");
    exp_weight.Text=="";  
    } 
}


But this only work for the number 0 or 9, for any number other then this it shows the message "Please enter Number Only".

Now can some body help me??

Thanks
Posted
Updated 12-Feb-11 1:49am
v3

The solution you use will depend on how you want the checking to be done.

If you want to prevent the user from being able to enter a non-numeric character then all of the previous solutions will work.

If you want the user to be able to enter what they want and check the input at the end then you should investigate the TextBox.Validating event.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Feb-11 22:21pm    
Good point, my 5.
--SA
Don't use a TextBox! Use a NumericUpDown instead! Voila, problem automatically solved.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 12-Feb-11 16:57pm    
My vote of 4:

You are right of course that it would a better way to go, but OP was specific about the TextBox part.
Upvoted to a five because some idiot thought he could vote a 2 without leaving reason. :)
Sergey Alexandrovich Kryukov 12-Feb-11 22:17pm    
I up-vote by my 5. Note just to oppose to the idiot (apparently idiot, is anyone disagree?).
Even though OP requested TextBox, how do we know that OP know what she or he really wants? In real life this is often not the case; so your answer gives better idea.
--Sa
Google or search CodeProject for 'Masked Textbox'

You need to use onKeyPress event and then probably use regular experession or ASCII values to verify the key pressed and act accordingly.
 
Share this answer
 
Comments
Olivier Levrey 12-Feb-11 7:35am    
My 5. MaskedTextBox is what OP needs although I don't think he has to use OnKeyPress: the Mask property is enough and does the job.
Sandeep Mewara 12-Feb-11 7:47am    
Details were to just explain how Masked textbox would work, such that in case OP wants can build own :)
MCY 12-Feb-11 12:13pm    
Easiest and most elegant way imho
Sergey Alexandrovich Kryukov 12-Feb-11 22:19pm    
No, masked text box can be very inconvenient, it is designed for more complex input.
Here, the task is simple, needs just filtering.
--SA
hanifuk 13-Feb-11 4:49am    
Exactly :)
This[^] solution may be another way of achieving this task.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Feb-11 22:21pm    
I voted 4 this time. The reference can give right idea, but the implementation should definitive (no options, there is only one which is adequate), also, the simple solution is not there.
--Sa
Abhinav S 13-Feb-11 0:21am    
I see what you mean.
Sergey Alexandrovich Kryukov 13-Feb-11 13:42pm    
Thanks for understanding.
--SA
Hi Hanifuk,

If you need to check if the user input is integer or not you can try this :

int i;
bool parsed = int.TryParse(Convert.ToString(textBox1.Text), out i);


if the value is integer the i variable will be set to the converted integer and the parsed boolean variable will be 'true'
else the i variable will be set to 0 and the parsed boolean variable will be 'false'

I hope this helps you,
:)
 
Share this answer
 
v2
Comments
Olivier Levrey 12-Feb-11 7:37am    
isn't 0 an int value? Unless 0 must be considered as an invalid value, it remains an int and therefore your solution is not enough... I would rather use the Parse method since it throws an exception if the value can't be converted into an int.
GenJerDan 12-Feb-11 7:45am    
Um...no. "i" be the integer value of the text if it was an "integer string". The return value is 0 or 1, depending on whether it successfully parsed.
Olivier Levrey 12-Feb-11 7:50am    
Check MSDN about the int parameter: "When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed.". What you are talking about the return value of the TryParse method: true or false whether the functions succeeds or not.
GenJerDan 12-Feb-11 18:13pm    
Yep. No 1 or 0. The number or 0, and a bool telling you its status.
Hi,
I think it can help:

C#
private void ok_Click(object sender, EventArgs e)
{
    int k;
    if(int.TryParse(exp_weight.Text, out k))
    {
        ok=true;
        this.Hide();
    }
    else
    {
        MessageBox.Show("Please Enter Number Only");
        exp_weight.Text=string.Empty;
    }
}
 
Share this answer
 

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