Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello, I would like to get help on an issue.

Briefly what I'm trying to do is make a calculator with a TextBox and a Button. I want it to give the result when "10+5" is typed into the TextBox.


What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            int number1, number2, result;
            string process = "";

            if (process == "+")
            {
                number1 = Convert.ToInt32(textBox1.Text);
                number2 = Convert.ToInt32(textBox1.Text);
                result = number1 + number2;

                MessageBox.Show(result.ToString());
            }
Posted
Updated 18-Sep-21 8:19am

Your question and code indicate you need to get a good book on C#, and study the basics of the language, as well as study how Controls, like TextBox, work.

I recommend a somewhat older book by Chris Sells: [^]

A TextBox [^] accepts user-entered characters, and contains a string: to do something like calculate, you need to transform that string into units that make sense ... like two numbers, and a character like +-/* that will indicate what operation you want tp perform on those numeric units.

And, what if the user types weird stuff you can't use, or leaves out stuff that's necessary; you have to deal with that; do you catch errors, reset when there is an error ?

One alternative would be to use two WinForms NumericUpDown [^] Controls for user number input, and another method for entering type of operation. Lots of different ways you could do that.
 
Share this answer
 
v2
You will have to read the string, break it into the various operators and operands, then process the formula as entered - once you have checked that it's a valid formula, that is.
What you have will not work at all.

There are a number of ways to do that, from regexes, through lexical analysis, to full blown execution engines - but since this is your homework I'd suggest that you start with what you know already, or it'll be far too obvious you cheated.

So think about what you know about strings, and how to "break them up" or consider them in parts, and start by working out how to get two numeric values and an operator out of a basic string "10+5".
When you have that working, think about what you know about maybe using a switch statement for deciding what operator you are working with.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You REALLY need to look at what you're doing and thinking about it instead of guessing.

What do you think this is going to do?
C#
number1 = Convert.ToInt32(textBox1.Text);
number2 = Convert.ToInt32(textBox1.Text);

You're converting the exact same content in a textbox twice. Do you really think you're going to get two different values?
 
Share this answer
 
v2
private void button1_Click( object sender, EventArgs e ) {

         int number1, number2, result;

         if ( textBox1.Text.Contains( "+" ) ) {

            var tokens = textBox1.Text.Split( "+" );

            if ( tokens.Length == 2 ) {
               bool ok = int.TryParse( tokens[ 0 ], out number1 ) &&
                  int.TryParse( tokens[ 1 ], out number2 );

               if ( ok ) {
                  result = number1 + number2;
                  MessageBox.Show( result.ToString() );
               }
            }
         }
      }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900