Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
hello!!
In my school fees structure project i need text boxes either contain string or int value so for that purpose i make my custom controls for numeric & for characters it is working properly but it is not taking space between two string...
my code
public class Class1 : TextBox
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
 

            if (char.IsNumber(e.KeyChar) == false)
            {
                if ((e.KeyChar == (char)Keys.Back)||(e.KeyChar==(char)Keys.Space))
                {
                    e.Handled = false;
 
                }
                else
                    e.Handled = true;
            }
        }
    }
 

    public class TextBoxAlphabetic : TextBox
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) == false)
            {
                if ((e.KeyChar == (char)Keys.Back)||(e.KeyChar==(char)Keys.Space))
                    e.Handled = false;
                else
                    e.Handled = true;
            }
        }
    }
 
should i use && oprator???
Posted 21-Mar-13 6:45am
Arifa S508

1 solution

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

Solution 1

"should i use && oprator??? "
No.
You can't replace the "||" with and "&&" - otherwise teh test will always fail as teh KeyChar value cannot both be Key.Back and Key.Space at the same time - for the same reason it can't be 'X' and '7' at the same time!
 
Put a breakpoint at the start of each handler, and follow it through - see what happens when you press the space bar and try to work it out from there.
 
BTW: Don't write it like this:
            if (char.IsNumber(e.KeyChar) == false)
INstead, use this:
            if (!char.IsNumber(e.KeyChar))
It's a lot more compact and more obvious once you get used to it!
  Permalink  
Comments
Member 9823122 at 21-Mar-13 12:41pm
   
Yes sir i did it thanks!!!!
OriginalGriff at 21-Mar-13 13:07pm
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 300
1 Mathew Soji 274
2 Afzaal Ahmad Zeeshan 268
3 DamithSL 225
4 Sergey Alexandrovich Kryukov 205
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2014
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