Solved

entering numbers in c# .NET

Posted on 2009-05-14
5
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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

Industry Leaders: 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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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