Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c# textbox validation

Posted on 2009-04-04
4
Medium Priority
?
498 Views
Last Modified: 2012-05-06
i wanna validate my textbox to only allow/accept interger
0
Comment
Question by:-Huey-
3 Comments
 
LVL 6

Accepted Solution

by:
Ramone_Hamilton earned 336 total points
ID: 24070849
Are you creating a webform or a webapplication.  If you are creating a web application use the numbervalidator control, if you are creating a winform you can check this on the KeyPress event. Look at the code below
protected void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    int numIsInt = 0; 
    e.Handled = !int.TryParse(e.KeyChar.ToString(), out numIsInt);
}

Open in new window

0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 332 total points
ID: 24071786
You could use the NumericUpDown control. Also have a look at http://www.emoreau.com/Entries/Articles/2003/03/Creating-your-own-Windows-Custom-Control.aspx
0
 
LVL 6

Assisted Solution

by:nandithaa
nandithaa earned 332 total points
ID: 24847735
Try this:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar > 47 && e.KeyChar < 58) || (e.KeyChar == 8))
            {
                e.Handled = false;
            }

            else
            {
                e.Handled = true;
            }
        }
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

782 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