Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello
i have a textBox than write in Event OnKeyPress below code

C#
protected override void OnKeyPress(KeyPressEventArgs e)
{
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
    base.OnKeyPress(e);
}


it only get integer
it don't get "." for decimal number for example 5.3
how i can change above code for my purpose? :):)
Posted
Updated 11-May-12 19:03pm
v4

If I understood your question correctly, to get the decimal separator you can use NumberFormatInfo.NumberDecimalSeparator Property[^]
 
Share this answer
 
Comments
faezun 11-May-12 14:24pm    
tanks
but i have a "Decimal TextBox"
Wendelius 11-May-12 16:54pm    
Do you mean that you accept only decimals? If that's the case then fetching the decimal separator and accepting that as input would be the answer as far as I understand. That's why I posted how to fetch the separator for numbers. You can use that as one acceptable input in your condition :)
faezun 12-May-12 1:01am    
Honey, I want a practical solution.
How?
Maciej Los 12-May-12 5:20am    
This is a practical solution. The decimal separator depends of Regional and Language option in your OS.
My 5!
Wendelius 12-May-12 5:48am    
Thanks losmac :)
C#
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
    {
        e.Handled = true;
    }

if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
 
Share this answer
 
Comments
faezun 12-May-12 5:00am    
Error 1 The name 'sender' does not exist in the current context
hi,


C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (!(e.KeyChar >= '0' && e.KeyChar <= '9' ||  e.KeyChar =='.'))

{
count=0;
if(!e.keychar=='.')
{
    count++;
    if(count>1)
        e.Handled=false;
}
else
e.Handled = true;
}
}
 
Share this answer
 
You can do the following:

C#
protected override void OnKeyPress(KeyPressEventArgs e)
{
    e.Handled = !(char.IsDigit(e.KeyChar) || (e.KeyChar == Keys.Decimal && !textBox.Text.Contains(".")) && !char.IsControl(e.KeyChar);
    base.OnKeyPress(e);
}


You will need to worry about multiple decimal points. You might want to check if the textbox already has a decimal point. I do not work with WinForms anymore, but the value in the textbox should be what the value is before the entry. I am also suspicious that there is no sender in the OnKeyPress method.
 
Share this answer
 
Comments
faezun 12-May-12 0:53am    
thanks
do you test it?
it have several errors!

Error 1 ) expected
Error 2 Operator '==' cannot be applied to operands of type 'char' and 'System.Windows.Forms.Keys'
Error 3 The name 'textBox' does not exist in the current context
Clifford Nelson 12-May-12 1:19am    
Could not test it since did not have WinForms application. Not even sure what the envirnement is since did not state the environment. Does not appear to be WinForm or WPF. the two i am most familiar with, and both have sender in the call. Gives you the idea though. If e.KeyChar == Keys.Decimal does not work, maybe you can just do e.KeycChar == '.'
Hi Friends,

This may help you ,,
try this below code...



C#
if (char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space || e.KeyChar==(char)Keys.Decimal)
            {
                
                e.Handled = false;
            }
            else
            {
                // Everything that is not a Digit, nor a backspace nor a space nor a decimal will be blocked 
                e.Handled = true;
            }
 
Share this answer
 
Comments
faezun 12-May-12 2:45am    
hi. Dear
thanks
but it don't get "."
thanks you all friends.
:):):)
final solution:

C#
int t=0;
protected override void OnKeyPress(KeyPressEventArgs e)
{
    string s = e.KeyChar.ToString();
    if (!(e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' && t < 1 || char.IsControl(e.KeyChar)))
    {
        e.Handled = true;
    }
    if (e.KeyChar == '.') t++;
    base.OnKeyPress(e);
}
 
Share this answer
 
Comments
faezun 14-May-12 5:04am    
hi friends.
a problem
if write in textBox and then delete it
textBox don't get "."
because counter t >0
how can set t=0 where textBox cleared?
:):):(
C#
using System.Text.RegularExpressions;


static bool AllNumeric(string inputString)
{
   return Regex.IsMatch(inputString, @"^\d+$");
}
 
Share this answer
 
Comments
CHill60 1-Jun-13 16:30pm    
It is a bit pointless adding a 10th solution to a question that is over a year old

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