Jump to content

How to store multiple values into a text box From buton

- - - - -

  • Please log in to reply
5 replies to this topic

#1
shyamenk

shyamenk

    Newbie

  • Members
  • PipPip
  • 11 posts
I just want to create a windows calculator.when i click digit 1 from calculator that value dispalyin the text box or calculator screen,whe i pree the second digit that replace the old digit in the text box
how can i solve this problem


code Below::::::

private void button20_Click(object sender, EventArgs e)
{



button20.Text = textBox1.Text = "1";



}

private void button21_Click(object sender, EventArgs e)
{
button21.Text = textBox1.Text = "2";
}
}

#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
Are you wanting to add the second button click to the first button click? If so the code would read:


textBox1.Text += button20.Text;


-CDG10620
Software Developer

#3
shyamenk

shyamenk

    Newbie

  • Members
  • PipPip
  • 11 posts
This Code is not suitable for me.....

i just want to add two numbers read from one text box seperately
read first number and it stores to one variable
read second value and store to next variable

add these two number.how can i solve this problem help me to find out the code............

#4
shyamenk

shyamenk

    Newbie

  • Members
  • PipPip
  • 11 posts
im extra adding one label text as zero

if (textBox1.Text == label1.Text)
button17.PerformClick();
Button clickedButton = (Button)sender;
textBox1.Text = textBox1.Text + clickedButton.Text;
a = Convert.ToInt32(textBox1.Text);

#5
iceman4154

iceman4154

    Newbie

  • Members
  • PipPip
  • 26 posts
There are many ways to create a calculator, like for instance a Windows calculator...
I would start by having a button to represent numbers 0-9, -, +, *,= and /. If a button representing a number is pressed it appends that button's value to your text box. If a operation button is pressed (+, -, * or /) then it takes the value of your text box, sets the operation to that value. After, you press another/more button(s) representing numbers and finally the = button is pressed which preforms the operation and sets the value of your text box to solution of that operation or equation.

My code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;


namespace Calculator

{

    public partial class Form1 : Form

    {

        private string sOperator = "";

        private string sFirstValue = "";


        public Form1()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            textBox1.Text += button1.Text;

        }


        private void button2_Click(object sender, EventArgs e)

        {

           textBox1.Text += button2.Text;

        }


        private void button3_Click(object sender, EventArgs e)

        {

            textBox1.Text += button3.Text;

        }


        private void button6_Click(object sender, EventArgs e)

        {

            textBox1.Text += button6.Text;

        }


        private void button5_Click(object sender, EventArgs e)

        {

            textBox1.Text += button5.Text;

        }


        private void button4_Click(object sender, EventArgs e)

        {

            textBox1.Text += button4.Text;

        }


        private void button9_Click(object sender, EventArgs e)

        {

            textBox1.Text += button9.Text;

        }


        private void button8_Click(object sender, EventArgs e)

        {

            textBox1.Text += button8.Text;

        }


        private void button7_Click(object sender, EventArgs e)

        {

            textBox1.Text += button7.Text;

        }


        private void button10_Click(object sender, EventArgs e)

        {

            textBox1.Text += button10.Text;

        }


        private void button14_Click(object sender, EventArgs e)

        {

            sFirstValue = textBox1.Text;

            textBox1.ResetText();

            sOperator = button14.Text;

        }


        private void button13_Click(object sender, EventArgs e)

        {

            sFirstValue = textBox1.Text;

            textBox1.ResetText();

            sOperator = button13.Text;

        }


        private void button12_Click(object sender, EventArgs e)

        {

            sFirstValue = textBox1.Text;

            textBox1.ResetText();

            sOperator = button12.Text;

        }


        private void button11_Click(object sender, EventArgs e)

        {

            sFirstValue = textBox1.Text;

            textBox1.ResetText();

            sOperator = button11.Text;

        }


        private void button15_Click(object sender, EventArgs e)

        {

            float Result = 0;


            switch (sOperator)

            {

                case  "+":

                    Result = float.Parse(sFirstValue) + float.Parse(textBox1.Text);

                    break;


                case "-":

                    Result = float.Parse(sFirstValue) - float.Parse(textBox1.Text);

                    break;


                case "*":

                    Result = float.Parse(sFirstValue) * float.Parse(textBox1.Text);

                    break;


                case "/":

                    Result = float.Parse(sFirstValue) / float.Parse(textBox1.Text);

                    break;

            }


            textBox1.ResetText();

            textBox1.Text = Convert.ToString(Result);

        }


        private void button16_Click(object sender, EventArgs e)

        {

            textBox1.ResetText();

        }


        private void button17_Click(object sender, EventArgs e)

        {

            textBox1.Text += button17.Text;

        }

    }

}


What we have here is a button to represent all numbers 0-9, +, -, *, /, . and =.
When a button is pressed it appends that value to the text box.
When a operation button is pressed it clears the text box and stores the first part of the equation.
When the = button is pressed it preforms the equation and appends the solution to the text box.

#6
salmon rushload

salmon rushload

    Newbie

  • Members
  • Pip
  • 4 posts
the stringbuilder class is pretty cool for situations where you have to keep updating a string.

here's another solution to your problem:


 public partial class Form1 : Form

    {

        StringBuilder st = new StringBuilder(); 


        public Form1()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            AddOp(1); 

        }


        private void button2_Click(object sender, EventArgs e)

        {

            AddOp(2); 

        }


        public void AddOp(int i)

        {

            


            st.Append(" " + i.ToString());


            string s = st.ToString(); 


            textBox1.Text = s; 

        }


        private void button3_Click(object sender, EventArgs e)

        {

            st.Append(" +");


            textBox1.Text = st.ToString(); 

        }

    }






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users