Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In c# form i put a text box. In that text box, i want only type numbers. If we press other buttons, it don't print in the text box. Do anybody knows a method?
Posted

How about using the NumericUpDown control instead?

Or you can check this out: Simple Numeric TextBox[^]
 
Share this answer
 
I would suggest you forget about changing the behavoir of the TextBox and use NumericUpDown
http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.aspx[^]

If you absolutely need a TextBox for some reason, then you can handle the KeyPress[^] event

C#
private void MyTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !char.IsDigit(e.KeyChar); 
}
 
Share this answer
 
v2
C#
private void tbxHeightBackImage_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);

        }
 
Share this answer
 
v2

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