How To Get Integer Value From Textbox In C#
Dwelling house and Learn: C# Internet Course
Getting Numbers From Text Boxes
Nosotros're going to change tack slightly, here. What we'll do is show yous how to become numbers from text boxes, so utilise these numbers in your code. Y'all'll demand to be able to practice this for your calculator project, which is coming up soon!
Start a new project for this one by clicking File > New Projection from the menu bar at the top of Visual C#.
Add a text box and a button to your new form. Set up the post-obit Properties for the text box (the tb below stands for text box):
Name: tbFirstNumber
Size: 50, xx
Location: 40, 35
Text: ten
And set the post-obit properties for your push:
Name: btnAnswer
Size: 75, 25
Location: 90, 90
Text: Answer
Your course volition then await like this:
What nosotros want to practise is to get that number 10 from the text box and display it in a bulletin box.
So double click your button to become at the coding window. Your cursor will be flashing inside of the button code. Set up 2 integer variables at the tiptop of the button lawmaking:
int firstTextBoxNumber;
int respond;
Your coding window should expect similar this:
To become at the number in the text box, we tin utilize the Text property of text boxes. Here's the line of code to add together:
firstTextBoxNumber = tbFirstNumber.Text;
This says, discover a text box called tbFirstNumber. Access its Text property. When the Text property is retrieved, shop it in the variable called firstTextBoxNumber.
To display the number in a bulletin box, add this line:
MessageBox.Testify( firstTextBoxNumber.ToString( ) );
Endeavour to Run your code. You should find C# won't run it at all. It volition give y'all the post-obit error:
With text boxes, the matter that you get is, non surprisingly, text. Nonetheless, nosotros're trying to store the text from the text box into an integer variable. C# won't let you do this - whole numbers belong in integer variables, non text. The error message is telling you that C# can't do the conversion from text to numbers for y'all - you take to practice it yourself!
So we demand to convert the text from the text box into an integer. The mode you lot practise this is to use something chosen Parsing. Fortunately, this involves nothing more complex that typing the word "Parse". You lot can do unlike types of Parses. Because we need to convert the text into an integer, nosotros need an Integer Parse. So change the line to this:
firstTextBoxNumber = int.Parse( tbFirstNumber.Text );
So you lot blazon int, so a full stop. From the IntelliSense carte, you can double click Parse. In betwixt a pair of circular brackets, you type the text you want to convert. In our case, the text is coming from a text box. But information technology doesn't have to. You can do this:
firstTextBoxNumber = int.Parse( "10" );
In the code higher up, the number is in double quotes. Double quotes hateful that it is text. Using int.Parse( ) means that it will be converted to a number that y'all tin can shop in an integer variable.
Run your programme and you lot'll discover that it works OK now. (You lot'll have a light-green wiggly line under reply, merely that'due south simply because we haven't used this variable notwithstanding.) Click your button and the number 10 will announced in the bulletin box. Type a different number in your text box, and click the button again. The new number should appear in place of the onetime ane.
You tin as well Parse other types of variable. Like this:
bladder firstTextBoxNumber;
firstTextBoxNumber = float.Parse( tbFirstNumber.Text );
Or this:
double firstTextBoxNumber
firstTextBoxNumber = double.Parse( tbFirstNumber.Text );
In the get-go one, we've ready a float variable. We've then used float.Parse( ) to catechumen the text from the text box, so that it tin can be stored in the float variable. Nosotros've done exactly the aforementioned matter in the second example, to convert the text into a double.
Things get more complicated if yous accidentally attempt to store a double value in a float variable - your programme will crash! You lot demand to effort to catch things similar this with lawmaking. (You'll see how to test for errors like this later in the book.)
For now, permit'south move on.
OK, then nosotros've got text from a text box and displayed information technology in a message box. What we'll exercise now is to add a second text box, get numbers from both, apply our Math operators, and do some calculations with the 2 number nosotros took from the text boxes. Sounds complex, but it isn't!
Add a second text box to your form. Set up the following Properties for information technology in the Properties Window:
Proper noun: tbSecondNumber
Size: l, 20
Location: 165, 35
Text: 5
Your form will and then look like this:
Double click the push to go at your code. Now fix up another integer variable to concord the second number from the new text box:
int secondTextBoxNumber;
To store the number from the text box in this new variable, add the following line:
secondTextBoxNumber = int.Parse( tbSecondNumber.Text );
This is the aforementioned every bit earlier - use int.Parse to convert the number from the text box into an integer variable. Then store the number in the new variable.
Permit's add the two numbers up, starting time. We tin use the answer variable for this. Hither'south the code to add:
answer = firstTextBoxNumber + secondTextBoxNumber;
And then we're just using the plus symbol ( + ) to add up whatever is in the two variables. The numbers in the variables come from the two text boxes.
Amend your bulletin box line to this:
MessageBox.Show( answer.ToString( ) );
All you lot need to do is to alter the proper noun of the variable that your are converting ToString( ).
Your coding window should look like ours:
Run your plan, then click the button. You lot should the answer to the improver in your message box:
Change the numbers in the text boxes, and click your push button over again. When you've finished playing with your new class, click the reddish 10 to render to the code. Here are a few exercises for you lot to endeavor.
Practise
Utilize the textboxes on your grade to calculate the following (y'all'll need to better your code for three of them):
1845 + 2858
3450 - 285
35 * 85
5656 / vii
(The answers you should get are: 4703, 3165, 2975 and 808.)
Exercise
Add a new text box to you form. Set up an integer variable to store a 3rd number. Become the third number from the text box and calculate the following:
(1845 + 2858) - 356
(3450 - 285) * 12
35 * ( 85 - 8 )
(5656 / 7) + 2156
(The answers you should become are: 4347, 37980, 2695 and 2964. You'll have to proceed closing the form down. And so add round brackets, the operator symbols, and the new variable.)
Once you've completed the exercises, yous can motility on to tackling the next project - your very own calculator!
<-- multiply and split up | a c# .net calculator project -->
back to the c# net contents page
How To Get Integer Value From Textbox In C#,
Source: https://www.homeandlearn.co.uk/csharp/csharp_s2p14.html
Posted by: valdezwhemere.blogspot.com
0 Response to "How To Get Integer Value From Textbox In C#"
Post a Comment