Click here to Skip to main content
Rate this: bad
good
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 9:24am
Comments
Wonde Tadesse at 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 at 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 at 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 at 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 at 30-Apr-11 15:03pm
   
Nice Answer! My 5 :)
Monjurul Habib at 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
0 Marcin Kozub 310
1 OriginalGriff 248
2 Sergey Alexandrovich Kryukov 215
3 Richard MacCutchan 202
4 Praneet Nadkar 197
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 30 Apr 2011
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