Click here to Skip to main content
12,299,907 members (53,738 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
can someone help me how to check that the user pressed an int ?
Posted 30-Apr-11 8:24am
Comments
Wonde Tadesse 30-Apr-11 14:39pm
   
On what type of application you talking about. Is it web site, console, window ?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

With the info you provided, I'd do something like this:

static void Main()
{
    object o1 = 10;
    object o2 = "20";
    object o3 = new Object();
 
    Console.WriteLine(CheckForInt(o1));
    Console.WriteLine(CheckForInt(o2));
    Console.WriteLine(CheckForInt(o3));        }
 
private static bool CheckForInt(object o)
{
    int dummy;
    return o is Int32 || Int32.TryParse(o.ToString(), out dummy);
}
  Permalink  
Comments
SAKryukov 30-Apr-11 17:23pm
   
I'm not sure this is what required, but I like the elegance of "||" instead of "&&". All correct, my 5.
--SA
Nishant Sivakumar 30-Apr-11 17:48pm
   
Thank you, SA!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It depends of where you entered the data, and when you whan to check if it is an int but with a textbox and checking everytime yo press a key may be something like this, add with VS a Keyup event to textbox and check the textBox.Text, then do what you need.




this.textBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox_KeyUp);
 

private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
    int _integer;
    if (!int.TryParse(textBox2.Text, out _integer)) MessageBox.Show("Not Int");
}

This is to check if whole data is a number, if you want to check key by key you can play with e.KeyCode.ToString().
Regards
  Permalink  
v3
Comments
anyavacy 1-May-11 6:38am
   
this is it, thank you
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

For C# console:
bool result = Int32.TryParse(value, out number);
  if (result)
  {
     Console.WriteLine("Converted '{0}' to {1}.", value, number);
  }
OR
C# Validating TextBox Control[^]

For asp.net, call any of the javascript functions :

Example 1:
function is_int(value){
   for (i = 0 ; i < value.length ; i++) {
      if ((value.charAt(i) < '0') || (value.charAt(i) > '9')) return false
    }
   return true;
}
Example 2 :
function is_int(value){
   return !isNaN(parseInt(value * 1)
}
Example 3:
function is_int(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
      return true;
  } else {
      return false;
  }
}

I hope the above information will be helpful. If you have more concerns, please let me know.
  Permalink  
Comments
RaviRanjankr 30-Apr-11 15:03pm
   
Nice Answer! My 5 :)
Monjurul Habib 30-Apr-11 15:04pm
   
thank you.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 30 Apr 2011
Copyright © CodeProject, 1999-2016
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