Click here to Skip to main content
15,868,101 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Comments
Sandeep Mewara 15-Feb-13 16:11pm    
Tried anything by yourself?

Hi
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsNumber(e.KeyChar))
    {
        e.Handled = false;
    }
}


Regards
Jegan
 
Share this answer
 
Comments
JayantaChatterjee 15-Feb-13 12:34pm    
This is good...
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

C#
int temp =0 ;
if(!int.TryParse(textbox1.text.Trim(),out temp))
MessageBox.Show("Enter integers only");
 
Share this answer
 
Comments
fjdiewornncalwe 15-Feb-13 9:47am    
+5.
Jegan Thiyagesan 15-Feb-13 11:01am    
This is very unattractive!
 
Share this answer
 
Comments
Shelby Robertson 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.
This function will do the test for you:

C#
// 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:

C#
if (IsNumeric(someVar))
{
    // success, do some stuff here
}
 
Share this answer
 
v3
Comments
Thomas Daniels 15-Feb-13 8:11am    
The OP tells his application is a Windows Application, and your answer is ASP.NET.
Nick Fisher (Consultant) 15-Feb-13 8:12am    
sorry, fixed now. :)
Thomas Daniels 15-Feb-13 8:24am    
Instead of
<small> 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);
} </small>

use the Double.TryParse method. This method returns true if the value is parsed successfully, otherwise the method returns false.
fjdiewornncalwe 15-Feb-13 9:51am    
Good call, ProgramFOX. Totally agree. Try-Catch should not be used as validation.
Thomas Daniels 15-Feb-13 9:57am    
Thank you!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900