Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
in textbox i want to enter only numbers,not characters using csharp how to validate in windows application.
 
i have one textbox called as follows;
 

Allocated Hrs textbox
 

in that textbox i want to type only numbers not characters.
 

for that how to validate using csharp in windows application.
 
please help me.
 
Note : it is windows application.
 
how can i do.please help me.
Posted 15-Feb-13 2:31am
Comments
Sandeep Mewara at 15-Feb-13 16:11pm
   
Tried anything by yourself?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi
 
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsNumber(e.KeyChar))
    {
        e.Handled = false;
    }
}
 
Regards
Jegan
  Permalink  
Comments
Jayanta Chatterjee at 15-Feb-13 12:34pm
   
This is good...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you can do it by 2 ways.
 
1. Use integer input box or masked text box (mask for integers only)
 
2. You can write a function for validating with syntax
 
int temp =0 ;
if(!int.TryParse(textbox1.text.Trim(),out temp))
MessageBox.Show("Enter integers only");
  Permalink  
Comments
Marcus Kramer at 15-Feb-13 9:47am
   
+5.
Jegan Thiyagesan at 15-Feb-13 11:01am
   
This is very unattractive!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
Comments
Shelby Robertson at 15-Feb-13 11:43am
   
This code project article seems to incorporate the "keydown" method suggested here plus also handles copy-paste situations that would be missed.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

This function will do the test for you:
 
    // returns if a variable is a numeric
    public System.Boolean IsNumeric(System.Object Expression)
    {
        if(Expression == null || Expression is DateTime)
            return false;
        if(Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean)
            return true;
   
        try 
        {
            if(Expression is string)
                Double.Parse(Expression as string);
            else
                Double.Parse(Expression.ToString());
                return true;
        } 
        catch (Exception e) 
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
        } 
        return false;
    }
 
 
Which can be used like:
 
            if (IsNumeric(someVar))
            {
                // success, do some stuff here
            }
  Permalink  
v3
Comments
ProgramFOX at 15-Feb-13 8:11am
   
The OP tells his application is a Windows Application, and your answer is ASP.NET.
Nick Fisher (Consultant) at 15-Feb-13 8:12am
   
sorry, fixed now. :)
ProgramFOX at 15-Feb-13 8:24am
   
Instead of
try
{
if(Expression is string)
Double.Parse(Expression as string);
else
Double.Parse(Expression.ToString());
return true;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}

use the Double.TryParse method. This method returns true if the value is parsed successfully, otherwise the method returns false.
Marcus Kramer at 15-Feb-13 9:51am
   
Good call, ProgramFOX. Totally agree. Try-Catch should not be used as validation.
ProgramFOX at 15-Feb-13 9:57am
   
Thank you!
Jegan Thiyagesan at 15-Feb-13 11:01am
   
Lot of unnecessary code!
 
Dont write code unless you absolutely need to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 200
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100