|
-
Jan 31st, 2003, 11:11 PM
#1
Thread Starter
Frenzied Member
Check for characters
This causes a fatal error if the input is not a number.
if ((float.Parse(x.Text) >- 1)||(float.Parse(x.Text) < 9999999))
How do I stop the the conversion if the user enters in a character?
How do I check to see if what was entered was a character or number?
-
Feb 1st, 2003, 12:20 AM
#2
Addicted Member
Throw a textBox on a form called textBox1 and give this a try.
PHP Code:
string outputStr = "";
foreach(char myChar in textBox1.Text)
{
if(Char.IsNumber(myChar))
{
outputStr += myChar.ToString();
}
}
textBox1.Text = outputStr;
textBox1.Select(textBox1.Text.Length, textBox1.Text.Length +1);
Jeremy
-
Feb 1st, 2003, 06:16 AM
#3
Frenzied Member
Put the above in a try catch block.
Dont gain the world and lose your soul
-
Feb 1st, 2003, 08:57 AM
#4
Just off the top of my head, wouldn't that cause a problem if the user entered a decimal number?
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Feb 1st, 2003, 09:00 AM
#5
Thread Starter
Frenzied Member
I'll have to give that a try and experimentation to see how it works.
-
Feb 1st, 2003, 09:23 AM
#6
Thread Starter
Frenzied Member
I Like that!!! It is something I was looking for anyways.
-
Feb 1st, 2003, 09:36 AM
#7
Thread Starter
Frenzied Member
I figured out a way around the . bug:
if(!Char.IsSymbol(myChar)&&(!Char.IsLetter(myChar)))
-
Feb 1st, 2003, 09:57 AM
#8
Thread Starter
Frenzied Member
But I can't get rid of the spaces. Any ideas?
-
Feb 1st, 2003, 12:54 PM
#9
PHP Code:
string outputStr = "";
bool bFoundDecimal = false;
foreach(char myChar in textBox1.Text)
{
if(Char.IsNumber(myChar))
{
outputStr += myChar.ToString();
}
else if ((!bFoundDecimal) && (myChar == '.'))
{
// there can only be one decimal!
outputStr += myChar.ToString();
bFoundDecimal = true;
}
}
textBox1.Text = outputStr;
textBox1.Select(textBox1.Text.Length, textBox1.Text.Length +1);
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Feb 1st, 2003, 01:24 PM
#10
Thread Starter
Frenzied Member
So I can substitute this
(myChar == '.')
with this
(myChar == ' ')
to get rid of the spaces and I won't forget to omit the bool for that.
Right?
-
Feb 1st, 2003, 01:31 PM
#11
Addicted Member
-
Feb 1st, 2003, 01:44 PM
#12
Thread Starter
Frenzied Member
Good! Will that work even if the user uses tabs instead of spaces. Or are tabs==spaces.
-
Feb 1st, 2003, 02:23 PM
#13
Addicted Member
i do not think that tabs are spaces think that tabs are '\t' characters.
Jeremy
-
Feb 1st, 2003, 07:47 PM
#14
Thread Starter
Frenzied Member
You may be right. But I hope that tabs are evaluated as one space at a time.
-
Feb 13th, 2003, 12:17 PM
#15
Junior Member
In C#, how to determine if the string is a number or not?
-
Feb 13th, 2003, 12:33 PM
#16
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 13th, 2003, 12:50 PM
#17
Junior Member
any.
i'm writting a program which highlight source code. because i want to check if the string is a number to mark it.
some examples:
3
3.12
-
Feb 13th, 2003, 03:11 PM
#18
What about hexadecimal?
Code:
bool isNumber(string str)
{
bool ret = str.Length > 0;
int hasPoint = 0;
foreach(char c in str) {
ret = ret && (char.IsDigit(c) || (c == '.' && hasPoint++ == 0));
}
return ret;
}
Note that this will fail if your string contains more than 2^32 '.'
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|