Click here to Skip to main content
13,193,816 members (48,887 online)
Rate this:
 
Please Sign up or sign in to 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 14-Oct-12 5:28am
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...
Wes Aday 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
Wes Aday 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...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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;
        }
    }
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

use regular expression /\b^[0-9]*\.?[0-9]*?$\b/ that only allow 0-9 and dot like 11.22
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Have a search on Google, that will help you!
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171018.2 | Last Updated 15 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100