none
adding multiple numbers from a single text box to get a total

    Question

  • Hi guys, I am trying to make a simple form with a 1x textbox, 1x label, 1x button. I would like to be able to put a number in the textbox 30 and click the button then add another number in textbox 50 and click the button then add another number in textbox 3 and in the label, the total would be there 83. Is it possible to do it in visual studio with a string?     

     
    Saturday, February 3, 2018 1:10 PM

All replies

  • I think that it is possible. In C#, the button's Click event handler is:

    string text_from_textbox = . . .;
    string text_from_label = . . .;
    int a, b;
    
    int.TryParse( text_from_label, out a );
    if( int.TryParse( text_from_textbox, out b ) )
    {
       string new_text_for_label = ( a + b ).ToString();
       // set label's text
       // . . .
    }


    Saturday, February 3, 2018 4:24 PM
  • This is obvious a assignment from your teacher. Very easy to do. 

    Therefore try it. If you are not able to do it, than overthink of programming is something not to difficult for you. 

    One hint, you have to set the characters from your textbox in a numeric format and vice verse.


    Success Cor

    Saturday, February 3, 2018 4:31 PM
  • Hi, I just tried the code I do get errors here (. . .;) I did try changing the code is below.

    Thanks in advance for your help.

    namespace add_from_textbox
    {
        public partial class <g class="gr_ gr_249 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="249" id="249">Form1 :</g> Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string text_from_textbox =  . . .;
                string text_from_label = . . .;

    ;
                int a, b;

                int.TryParse(text_from_label, out a);
                if (int.TryParse(text_from_textbox, out b))
                {
                    string new_text_for_label = (a + b).ToString();
                    // set label's text
                    // . . .
                }
            }

            private void label_Click(object sender, EventArgs e)
            {

            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }
        }
    }


    • Edited by vautek Saturday, February 3, 2018 5:00 PM
    Saturday, February 3, 2018 4:58 PM
  • I have the textbox, label and button on the form and the code as you have posted with the change to . . . ; I get no errors and the calculation is not working the label has not changed is there something I have missed.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string text_from_textbox = label.Text;
                string text_from_label = label.Text;
                int a, b;
    
                int.TryParse(text_from_label, out a);
                if (int.TryParse(text_from_textbox, out b))
                {
                    string new_text_for_label = (a + b).ToString();
                    // set label's text
                    // . . .
                }
            }
    
            private void textBox_TextChanged(object sender, EventArgs e)
            {
    
            }
        }
    }


    • Edited by vautek Sunday, February 4, 2018 12:17 PM
    Sunday, February 4, 2018 12:14 PM
  • Hi vautek,

    Welcome to the MSDN forum.

    This forum is discussing Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor.

    According to your description, your issue is related to C# Development, I will move this thread to corresponding forum for a professional answer.

    Thanks for your understanding.

    Regards,

    Judyzh


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 5, 2018 8:32 AM
  • Hello,

    Try the following

    using System;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                label1.Text = Array.ConvertAll(textBox1.Text.Split(' '), new Converter<string, int>((input) => {
                    int output = 0;
    
                    int.TryParse(input, out output);
    
                    return output;
                })).Sum().ToString();
            }
        }
    }
    

    A simple insertion of some text this time.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 5, 2018 10:42 AM
  • Hi Karen, what I a trying to do is from form1 add many times to get a total

    add 50

    click button

    then add 20

    click button

    and every time I add a number it adds to the label

    thanks in advance for your help

    Monday, February 5, 2018 1:45 PM
  • The code from Viorel_MVP looks like it should work but I have not been able to get it working. Thanks in advance for your help guys
    Monday, February 5, 2018 1:47 PM
  • I think your problem probably lies here:

    string text_from_textbox = label.Text;
    string text_from_label = label.Text;

    The first one should be textbox.Text


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, February 6, 2018 2:16 AM
  • Hi vautek,

    Try this:

            double num = 0;
            private void button1_Click(object sender, EventArgs e)
            {
                num += Convert.ToDouble(textBox1.Text);
                label1.Text = num.ToString();
                textBox1.Text = "";
            }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 6, 2018 2:58 AM
    Moderator

  •         double num = 0;
            private void button1_Click(object sender, EventArgs e)
            {
                num += Convert.ToDouble(textBox1.Text);
                label1.Text = num.ToString();
                textBox1.Text = "";
            }

    Sorry, Stanly ... it's a bad idea to use a Convert method when the data comes from a user-entered value. If something other than a number is entered, that Convert will throw an Exception. It's always better to use a TryParse().


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, February 6, 2018 5:34 AM
  • Hi Bonnie,

    Yes you're right, the solution provided by Viorel is better, thanks for your reminder.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 6, 2018 6:07 AM
    Moderator
  • That works nicely thank you for your help
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            double num = 0;
            private void button1_Click(object sender, EventArgs e)
            {
                num += Convert.ToDouble(textBox1.Text);
                label1.Text = num.ToString();
                textBox1.Text = "";
            }
        }
    }
    

    Tuesday, February 6, 2018 8:39 AM
  • Hi vautek,

    Just as Bonnie said, it will be better to refer to the solution provided by Viorel that:

            private void button1_Click(object sender, EventArgs e)
            {
                string text_from_textbox = textBox1.Text;
                string text_from_label = label1.Text;
                int a, b;
    
                int.TryParse(text_from_label, out a);
                if (int.TryParse(text_from_textbox, out b))
                {
                    label1.Text = (a + b).ToString();
                }
            }

    Note: If my reply provided you with a solution please mark the reply as answered as this will help others looking for the same or similar issues down the road.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 6, 2018 8:43 AM
    Moderator