Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I think this question is really easy, but nonetheless, i coulnd't find it.

My current textbox has the ability to accept numbers, and backspace.
But the input has to be a float variabel, whitch means that i'll need dots (comma's?) as well.
private void tb_Stash_KeyPress(object sender, KeyPressEventArgs e)
{
  if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
    e.Handled = true; 
}

What can I do to acomplish this?
Posted
Updated 30-Mar-11 1:20am
v2

Make an extension method that does the appropriate comparison:

C#
public static class ExtensionMethods
{
    public static bool IsCharacter(this char c, char thatChar)
    {
        return (c == thatChar);
    }
}


And call it this way:

C#
e.Handled = (!char.IsDigit(e.KeyChar)   && 
             !char.IsControl(e.KeyChar) && 
             !char.IsCharacter(System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator.GetAt(0)));


If you want to, you can also account for the CurrencyGroupSeparator.
 
Share this answer
 
v2
Comments
RaviRanjanKr 30-Mar-11 14:59pm    
Nice Answer. my 5 :)
Something like this would do it for you for any kind of culture ('.' or ','):

C#
private void tb_Stash_KeyPress(object sender, KeyPressEventArgs e)
{
  System.Globalization.CultureInfo c = System.Globalization.CultureInfo.CurrentUICulture;
  char dot = (char)c.NumberFormat.NumberDecimalSeparator[0];
  if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !e.KeyChar.Equals(dot))
    e.Handled = true;
}
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 30-Mar-11 7:36am    
That's good! 5+
You could use a different approach that will be more lenient to changing requirements due to locale. Try to parse what has been input into the TextBox with Double.TryParse(...) and accept the input only when parsing was successfull:

C#
private void tb_Stash_KeyPress(object sender, KeyPressEventArgs e)
{
    String alreadyInput = tb_Stash.Text;
    String wouldBe      = alreadyInput + e.KeyChar;
    Double shouldBe;
    if(!Double.TryParse(wouldBe, out shouldBe))
    {
        e.Handled = true;
    }
    else
    {
        //You could do something with shouldBe here if you wanted
    }
}


Best Regards,
 
Share this answer
 
Comments
Amateurgrammer 30-Mar-11 7:34am    
Thanks, i'll try this right away.
lukeer 30-Mar-11 9:35am    
I like the TryParse() approach. But for this to work, you would need special treatment for "Backspace" inputs.
Manfred Rudolf Bihy 30-Mar-11 9:44am    
You are right, but I think that should not be too hard for OP to accomplish. :)
if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back 
& e.KeyChar != '.') 

I think comma will not be needed
 
Share this answer
 
Comments
Manfred Rudolf Bihy 30-Mar-11 7:35am    
It is where I come from since the decimal separator in Germany is the comma and not the period.

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