[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

entering numbers in c# .NET

Posted on 2009-05-14
5
Medium Priority
?
432 Views
Last Modified: 2012-08-13
hi,
           I have one phone number text box.In that it takes numbers only.it is working fine.i takes only numbers. But Back Space is not working.I post my code
private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            int isNumber = 0;
            
            e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);
 
        }

Open in new window

0
Comment
Question by:mannevenu
5 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24392792
I think you should "specialize" your text box by using validators:
http://www.codeproject.com/KB/validation/validationprovider.aspx
You can use some masks and the System.Text.RegularExpressions.RegEx class like here: (search for RegEx)
http://www.cornetdesign.com/2008/01/tdd-of-winform-app-part-5-validating.html
See the MSDN doc here : http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

Let's have a look to the System.Text.RegularExpressions namespace and to the System.Windows.Forms.ErrorProvider provider class :
http://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider.aspx
There are some complete example.

Nevertheless, if you really wants to continu this way, e.KeyChar.ToString() is a string so you can use its Trim methods : http://msdn.microsoft.com/en-us/library/system.string_members.aspx
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24392805
So as I said Trim methods without any argument "Removes all leading and trailing white-space characters from the current String object" : http://msdn.microsoft.com/en-us/library/t97s7bs3.aspx
Probably I can write something like this:
e.Handled = !int.TryParse(e.KeyChar.ToString().Trim(), out isNumber);
 But I'm still convinced that you should develop something like the codeproject article shows above.
0
 
LVL 10

Accepted Solution

by:
Marcjev earned 2000 total points
ID: 24392929
Or use something like this validation in a derived textbox or your implementation.
You should only handle your allowed characters though...
public class NumericTextBox : TextBox
{
    bool allowSpace = false;
 
    // Restricts the entry of characters to digits (including hex), the negative sign,
    // the decimal point, and editing keystrokes (backspace).
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);
 
        NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
        string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
        string groupSeparator = numberFormatInfo.NumberGroupSeparator;
        string negativeSign = numberFormatInfo.NegativeSign;
 
        string keyInput = e.KeyChar.ToString();
 
        if (Char.IsDigit(e.KeyChar))
        {
            // Digits are OK
        }
        else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
         keyInput.Equals(negativeSign))
        {
            // Decimal separator is OK
        }
        else if (e.KeyChar == '\b')
        {
            // Backspace key is OK
        }
        //    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
        //    {
        //     // Let the edit control handle control and alt key combinations
        //    }
        else if (this.allowSpace && e.KeyChar == ' ')
        {
 
        }
        else
        {
            // Consume this invalid key and beep
            e.Handled = true;
            //    MessageBeep();
        }
    }
 
    public int IntValue
    {
        get
        {
            return Int32.Parse(this.Text);
        }
    }
 
    public decimal DecimalValue
    {
        get
        {
            return Decimal.Parse(this.Text);
        }
    }
 
    public bool AllowSpace
    {
        set
        {
            this.allowSpace = value;
        }
 
        get
        {
            return this.allowSpace;
        }
    }
}

Open in new window

0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24396259
is it a web application or a windows application
0
 

Author Comment

by:mannevenu
ID: 24408938
Windows application
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question