Solved

c# textbox validation

Posted on 2009-04-04
4
484 Views
Last Modified: 2012-05-06
i wanna validate my textbox to only allow/accept interger
0
Comment
Question by:-Huey-
[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
4 Comments
 
LVL 6

Accepted Solution

by:
Ramone_Hamilton earned 84 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 83 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 83 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

622 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