Solved

IsNumber method in C#?

Posted on 2001-08-22
9
3,757 Views
Last Modified: 2008-02-01
Given:
string str = textbox1.Text

Can you determine if the string is numeric in C# by doing something along the lines of:

IsNumber(str)
{
   try
   {
      int.Parse(str)
      return true;
   }
   catch
   {
      (System.FormatException)
      return false;
   }
}
I kind of made that up as I went along.
I have also been toying with the notion of checking the Ascii value of the string, but did not get very far with that idea.
A little help here would be appreciated.

bruwmac
0
Comment
Question by:bruwmac
  • 4
  • 4
9 Comments
 
LVL 6

Expert Comment

by:MichaelS
ID: 6416622
Did you checked functions
isalpha() and isdigit()?

I am not really familiar with C# but hope you can use it.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6417231
in C# it is IsDigit and IsLetter they both can work n a char or an ofsset into a supplied string - there is also IsNumber which can be used for decimal or hex - there are many other Is... functions
0
 

Author Comment

by:bruwmac
ID: 6417594
I checked for IsDigit, IsLetter, and IsNumber in the help index. With the Visual C# filter, they  aren't indexed, though they show up under Visual C# and related filter. However, I can't seem to get them to go.
ShaunWilde, if you recall my sort method you helped me with yesterday, I am trying to check the textbox input for numeric input.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6417968
are you looking for a number or just a single digit

eg a string of 23764523764 being converted to a number

or just lookig at an element eg [0] = 2 , [3] = 6
0
 

Author Comment

by:bruwmac
ID: 6418119
The Sort method called within my method sorts the strings entered into the textbox based on the first character of the string. If the string happens to be a number, 2006 will erroneously be sorted before 378, based on the the first characters, 2 being smaller than 3. If I can test the first character of the string to determine if it is numeric, I can fire off a messagebox saying "Please do not enter numbers."
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6418225
ah - You may have to handle an event such as OnTextChanged and then look at the first character in the string using IsDigit(Text,0) - I'll check when I get home in a couple of hours
0
 

Author Comment

by:bruwmac
ID: 6418854
TextChanged event was a good clue.
I did the following:

txtString1_TextChanged (...etcetera)
{
   try
   {
      int.Parse(txtString1.Text);
      MessageBox.Show("Please, do not enter numbers!");
   }
   catch(System.FormatException) // when a non-numeric is entered
   {
   }
}

This works slick. No IsAnything required. But, I ask you, is this proper programming, or am I quietly creating unknown problems? I don't want to blow-up any NASA rockets.

   
 
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 100 total points
ID: 6419347
its a known way - some people wait till you click on OK and then validate the fields entered others might handle key presses and not let you enter a number till the end

I think a good way would be to validate when the edit control loses focus eg (ps I have just found the events (the lightening symbol) I knew it had to be in there somewhere :) )

          protected void textBox1_Leave (object sender, System.EventArgs e)
          {
               // do you checin in here
               // if bad text you can throw focus back to the control to make them reenter it
               
          }
0
 

Author Comment

by:bruwmac
ID: 6419663
Thanks for the tip. I guess that wraps this topic up.
I have a couple of dialog boxes I add to most of my projects and I find myself recreating them over and over again. I know I can import them somehow. Has not worked, though. Missing references or something. An answer to my similar question, Combining projects in C#, in the general programming section would help me out with this.
Take care.

bruwmac
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is the real difference between C++ and Visual C++.NET? 7 114
Socket Programming (Unix) 8 124
GUI: DIalog Stacking and Popping in MS C++ 4 74
FMX TCameraComponent Problem 2 68
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

815 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

13 Experts available now in Live!

Get 1:1 Help Now