Results 1 to 2 of 2

Thread: Adding values in one text box and displaying in another

  1. #1
    Exteez is offline C# Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2017
    Posts
    1
    Rep Power
    0

    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):
    4
    5
    1
    2

    Single line text box (textBox4.Text):
    12
    Thank you for helping me with my code!

    Regards, Exteez

  2. #2
    jmcilhinney is offline C# Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,442
    Rep Power
    116
    Quote Originally Posted by Exteez View Post
    [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.
    Quote Originally Posted by Exteez View Post
    [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
    .Sum(s =>
    {
    int number;

    int.TryParse(s, out number);

    return number;
    })
    .ToString();

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •