Solved

c# textbox validation

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

Accepted Solution

by:
Ramone_Hamilton earned 84 total points
Comment Utility
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 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 83 total points
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now