Adding values in one text box and displaying in another
Hi guys, I have two text boxes in my windows form application on visual studio 2015 in C#. One is a multi line text box, one is a single line text box. The multi line text box is called textBox5.Text, and the single line text box is called textBox4.Text. I would like to enter a lot of numbers in the multi line text box (one value per line). Then what I want is so that all the values from the multi line text box are added up and displayed in the single line text box as one value AUTOMATICALLY. This would be best done without a button, however if there isn't a choice a button would do as well.
I will give an example here:
Multi line text box(textBox5.Text):
Single line text box (textBox4.Text):
Thank you for helping me with my code! Regards, Exteez
[COLOR=#666666][FONT="]The multi line text box is called textBox5.Text, and the single line text box is called textBox4.Text.
First things first, you should fix that. If someone was to look at code using those names, they'd have no idea what they were actually for. All your controls and everything else should have descriptive names.
Originally Posted by Exteez
[COLOR=#666666][FONT="]This would be best done without a button, however if there isn't a choice a button would do as well.
What does the Button actually do? It simply provides an event on which you can perform an action. The user clicks the Button and the Button raises its Click event. You handle the Click event and perform the action. If you want to perform the action when the text changes in the TextBox rather than when the user clicks a Button, what event do you suppose you should handle?
As for the question, you should start with the Lines property of the multiline TextBox. That returns a String array where each element is a line from the TextBox. You can then convert each String to an int and sum them, then display that sum. If you want to do it properly, you need to account for the fact that the user might enter values that are not numeric or no value at all on some lines. As a beginner, you should probably do it with a loop but the most succinct way is with LINQ:
sumTextBox.Text = valuesTextBox.Lines