Click here to Skip to main content
12,072,067 members (57,780 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL-server-2005 .NET3.5 C# .NET , +
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 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2016
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