Click here to Skip to main content
11,412,834 members (75,433 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GimmeCode Homework
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 6:28am
Edited 14-Oct-12 6:32am
v2
Comments
Kenneth Haugland at 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 at 14-Oct-12 11:33am
   
...this week
Kenneth Haugland at 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 at 14-Oct-12 11:34am
   
I am wondering what have you tried?
Kenneth Haugland at 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 at 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
0 OriginalGriff 399
1 Sascha Lefévre 200
2 Maciej Los 180
3 ProgramFOX 130
4 Sergey Alexandrovich Kryukov 110
0 Sergey Alexandrovich Kryukov 9,025
1 OriginalGriff 7,317
2 Maciej Los 3,570
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web04 | 2.8.150427.1 | Last Updated 15 Oct 2012
Copyright © CodeProject, 1999-2015
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