Connecting Tech Pros Worldwide Help | Site Map

Converting a string to an int - Easy Question

robert.b.walden@gmail.com
Guest
 
Posts: n/a
#1: Sep 6 '06
Hello,

Easy question from a newbie. I have text boxes which collect users
input and on the click of a button do some calculations and output to
another text box. I need to easily convert the numbers inputted to
int's so I can do the calculations. Also I need to figure out how to
pop up an error box if the charactors entered in those boxes are
anything but numbers. Thanks in advance for helping someone trying to
learn this robust language.

Cor Ligthert [MVP]
Guest
 
Posts: n/a
#2: Sep 6 '06

re: Converting a string to an int - Easy Question


Robert,

There are in C# at least three standard methods for that
The Parse and the Convert

String Parse
http://msdn.microsoft.com/library/en...arseTopic1.asp

Convert.ToInt
http://windowssdk.msdn.microsoft.com.../sf1aw27b.aspx

TryParse
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx

These all are overloaded by the way.

I hope this helps,

Cor



<robert.b.walden@gmail.comschreef in bericht
news:1157512244.264531.49940@b28g2000cwb.googlegro ups.com...
Quote:
Hello,
>
Easy question from a newbie. I have text boxes which collect users
input and on the click of a button do some calculations and output to
another text box. I need to easily convert the numbers inputted to
int's so I can do the calculations. Also I need to figure out how to
pop up an error box if the charactors entered in those boxes are
anything but numbers. Thanks in advance for helping someone trying to
learn this robust language.
>

Morten Wennevik
Guest
 
Posts: n/a
#3: Sep 6 '06

re: Converting a string to an int - Easy Question


In addition, you can block all non-digit characters by handling the
KeyPress event and checking for Char.IsDigit (and Char.IsControl).


On Wed, 06 Sep 2006 05:56:10 +0200, Cor Ligthert [MVP]
<notmyfirstname@planet.nlwrote:
Quote:
Robert,
>
There are in C# at least three standard methods for that
The Parse and the Convert
>
String Parse
http://msdn.microsoft.com/library/en...arseTopic1.asp
>
Convert.ToInt
http://windowssdk.msdn.microsoft.com.../sf1aw27b.aspx
>
TryParse
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx
>
These all are overloaded by the way.
>
I hope this helps,
>
Cor
>
>
>
<robert.b.walden@gmail.comschreef in bericht
news:1157512244.264531.49940@b28g2000cwb.googlegro ups.com...
Quote:
>Hello,
>>
>Easy question from a newbie. I have text boxes which collect users
>input and on the click of a button do some calculations and output to
>another text box. I need to easily convert the numbers inputted to
>int's so I can do the calculations. Also I need to figure out how to
>pop up an error box if the charactors entered in those boxes are
>anything but numbers. Thanks in advance for helping someone trying to
>learn this robust language.
>>
>
>


--
Happy Coding!
Morten Wennevik [C# MVP]
dongmt@gmail.com
Guest
 
Posts: n/a
#4: Sep 6 '06

re: Converting a string to an int - Easy Question


in the button _ click event (double click the button to see code), you
write

try {

int num=Convert.toint32(textboxinput.text)
num=num+10; // or plus with else value ...
textboxoutput.text=num.tostring();
}
catch
{
MessageBox.show(" the data input is not in right format");
}




robert.b.walden@gmail.com viết :
Quote:
Hello,
>
Easy question from a newbie. I have text boxes which collect users
input and on the click of a button do some calculations and output to
another text box. I need to easily convert the numbers inputted to
int's so I can do the calculations. Also I need to figure out how to
pop up an error box if the charactors entered in those boxes are
anything but numbers. Thanks in advance for helping someone trying to
learn this robust language.
Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#5: Sep 6 '06

re: Converting a string to an int - Easy Question


Hi,

This is trivial to do in 2.0 just use MaskedTextbox instead of a regular
Textbox

In 1.X you can either handley the KeyDown event or modify a protected
property for the later you need to derive from Textbox


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


<robert.b.walden@gmail.comwrote in message
news:1157512244.264531.49940@b28g2000cwb.googlegro ups.com...
Quote:
Hello,
>
Easy question from a newbie. I have text boxes which collect users
input and on the click of a button do some calculations and output to
another text box. I need to easily convert the numbers inputted to
int's so I can do the calculations. Also I need to figure out how to
pop up an error box if the charactors entered in those boxes are
anything but numbers. Thanks in advance for helping someone trying to
learn this robust language.
>

Closed Thread