Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 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
C#
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

1 solution

"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:
C#
if (char.IsNumber(e.KeyChar) == false)
INstead, use this:
C#
if (!char.IsNumber(e.KeyChar))
It's a lot more compact and more obvious once you get used to it!
 
Share this answer
 
Comments
Arifa S 21-Mar-13 12:41pm    
Yes sir i did it thanks!!!!
OriginalGriff 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900