• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 733
  • Last Modified:

add two textboxes to show sum in third textbox

I am using visual studio 2008 express-
I have three textboxes this.textBox1, this.textBox2, and this.textBox3 and a button.
How do I code button to add textbox1 and textBox2 to display in textBox3.
Thank you
0
davetough
Asked:
davetough
  • 3
  • 2
2 Solutions
 
zadeveloperCommented:
in the designer doube click on the button and it will create a event function for you.

Then type in:



textBox3.Text =  textBox1.Text + " " + textBox2.Text

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I would do something more like:
(replacing 'int' with whatever datatype is appropriate for your input)
        private void button1_Click(object sender, EventArgs e)
        {
            int x, y;
            if (int.TryParse(this.textBox1.Text, out x))
            {
                if (int.TryParse(this.textBox2.Text, out y))
                {
                    int z = x + y;
                    this.textBox3.Text = z.ToString();
                }
                else
                    MessageBox.Show("Invalid int in textBox2");
            }
            else
                MessageBox.Show("Invalid int in textBox1");
        }

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
* The word "sum" was used in the description implying that numbers were being input.  =)
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
zadeveloperCommented:
:) - good eyes, I missed that totally.
0
 
zadeveloperCommented:
Just another variation of how ID: 26206376 could be written

the int does not allow decimal places where as the decimal does (but same thing)

decimal x, y;

            if (decimal.TryParse(this.textBox1.Text, out x) &&
                decimal.TryParse(this.textBox2.Text, out y))
            {
                textBox3.Text = (x+y).ToString();
            }
            else
                textBox3.Text = "Invalid entry in textbox 1 or 2";

Open in new window

0
 
davetoughAuthor Commented:
thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now