Results 1 to 10 of 28

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #18
    Banned

    Join Date
    Aug 2012
    Country
    Posts
    679
    Thanked
    394
    Thanks
    615
    Try this.

    Code:
    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
        {
    
            private readonly int firstbox;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            public void button1_Click(object sender, EventArgs e)
            {
                int firstbox = Int32.Parse( textBox1.Text ); //here you input first value
                int firstvalue = Int32.Parse( textBox1.Text ); // this is to store the first value you write in textbox
                textBox1.Text = ""; //this is so that the textbox clears after the click
            }
    
    
            public void button2_Click(object sender, EventArgs e)
            {
                int secondbox = Int32.Parse( textBox1.Text );  //second input!
                int secondvalue = Int32.Parse( textBox1.Text ); //this is to store the second value that you input after buttonclick
                int result = firstbox + secondbox; //the sum
                int limitbox = Int32.Parse( textBox2.Text ); //limit!
    
                if (result <= limitbox)
                { //if you only write < and the MAX is 20, it will give you Invalid because 20 is not less than 20
                    textBox1.Text = result.ToString();
                }
                else
                {
                    MessageBox.Show("Invalid input");
                }
    
    
            }
        }
    }
    I made firstbox an instance variable otherwise it would have been out of context.

    Remember to edit the namespace. You may also need to remove readonly.
    Last edited by Paul; 10-16-2015 at 10:52 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)