473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Converting a string to an int - Easy Question

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.

Sep 6 '06 #1
4 1897
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

<ro*************@gmail.comschreef in bericht
news:11*********************@b28g2000cwb.googlegro ups.com...
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.

Sep 6 '06 #2
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]
<no************@planet.nlwrote:
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

<ro*************@gmail.comschreef in bericht
news:11*********************@b28g2000cwb.googlegro ups.com...
>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]
Sep 6 '06 #3
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");
}


ro*************@gmail.com viết :
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.
Sep 6 '06 #4
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
<ro*************@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
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.

Sep 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bill Sneddon | last post by:
I am using an XML file produced by doing a save-as in Excel. The file has content that looks like one of these three examples lines: <Cell ss:StyleID="s24"><ss:Data ss:Type="String"...
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
3
by: Parvesh | last post by:
hi, I am using a webservice which Returns the Result in an XML string, The XML response i get i svery cumbersome to parse, But if i could convert it to the Corresponding Class using the...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
5
by: Jeff Dillon | last post by:
How might I convert a string like 10.A (in hex) to it's decimal equivalent? Basically I have an input string like ((1F.A + 3A.D) - 1F.E) and need to calculate the result. Using Reflection and...
3
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.