PC Review


Reply
Thread Tools Rate Thread

Converting a string to an int - Easy Question

 
 
robert.b.walden@gmail.com
Guest
Posts: n/a
 
      6th Sep 2006
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.

 
Reply With Quote
 
 
 
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      6th Sep 2006
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



<(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      6th Sep 2006
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]
<(E-Mail Removed)> wrote:

> 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
>
>
>
> <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed)...
>> 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]
 
Reply With Quote
 
dongmt@gmail.com
Guest
Posts: n/a
 
      6th Sep 2006
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");
}




(E-Mail Removed) 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.


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      6th Sep 2006
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting string to method, templates question damian@gcoders.com Microsoft C# .NET 6 19th Feb 2007 07:04 PM
char string question - easy Keith Smith Microsoft C# .NET 4 14th Feb 2005 03:12 PM
Question on converting char* into System.String Yan Vinogradov Microsoft C# .NET 4 28th Oct 2004 02:28 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Jay B. Harlow [MVP - Outlook] Microsoft C# .NET 7 1st Aug 2003 06:03 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Mikael Jansson Microsoft C# .NET 0 31st Jul 2003 08:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:40 AM.