Results 1 to 10 of 28

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    qais187's Avatar Junior Member

    Join Date
    Nov 2014
    Country
    Posts
    93
    Thanked
    110
    Thanks
    15
    Originally Posted by Paul View Post
    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.
    It still says "The name 'firstbox' does not exist in the current context" im using xaml if its making a difference


  3. #3
    Banned

    Join Date
    Aug 2012
    Country
    Posts
    679
    Thanked
    394
    Thanks
    615
    Originally Posted by qais187 View Post
    It still says "The name 'firstbox' does not exist in the current context" im using xaml if its making a difference
    Oh, you're using WPF. Yes it does, make a difference. Thought you were using winforms.

    It shouldn't be out of context. I never receoeved that error in my debugger.

Thread Information

Users Browsing this Thread

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