Click here to Skip to main content
16,004,678 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm wondering how to make a text-box only accept 1s, 2s, 3s, 4s, 5s, 6s, 7s, 8s, 9s, 0s, and.s.
Posted
Updated 14-Oct-12 5:32am
v2
Comments
Kenneth Haugland 14-Oct-12 11:32am    
Check the preview of the text change, if its not in your group than stop it, and if it is then don't stop it. And the questrion must have been asked 10 times here on this site before...
[no name] 14-Oct-12 11:33am    
...this week
Kenneth Haugland 14-Oct-12 11:35am    
Its really easy to do also... Once you get the point that you could stop a key down on the textbox
[no name] 14-Oct-12 11:34am    
I am wondering what have you tried?
Kenneth Haugland 14-Oct-12 11:36am    
I must confess I haven't tried this one, I'm not sure if its WinForms or WPF so...

C#
private void tbacc_Receipt_nAmount_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.')
    {
        e.Handled = true;
    }

    if (!char.IsControl(e.KeyChar))
    {

        TextBox textbox = (TextBox)sender;

        if (textbox.Text.IndexOf('.') > -1 &&
                 textbox.Text.Substring(textbox.Text.IndexOf('.')).Length >= 3)
        {
            e.Handled = true;
        }
    }
}
 
Share this answer
 
 
Share this answer
 
You can use NumericUpdown for this.

You can hide up and down arrow with this code
NumericUpdown1.Controls[0].Hide();

for disable increment and decrement :
set Increment property to zero

for use decimal values:
set DecimalPlaces 1 or more

and you can not use e value

don't forget to set Maximum and Minimum values

it seems textbox but only accept numeric values
 
Share this answer
 
v3
use regular expression /\b^[0-9]*\.?[0-9]*?$\b/ that only allow 0-9 and dot like 11.22
 
Share this answer
 
Have a search on Google, that will help you!
 
Share this answer
 
Comments
Curtdawg123 14-Oct-12 12:09pm    
Thanks, I figured it out

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