0

I have to make this method based calculator for my C# class. I'm using Visual Studio 2010 and part of the assignment is to have the "Calculate" button method go when the user presses enter.

I've done this before in VS but for some reason this time it just keeps dinging when I try the enter key.

Here's my code. I don't think it has anything to do with the code, but I have no idea what the problem is so I'm posing it just in case.

Any help is much appreciated. Thank you!

{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 Week4Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            double operand1 = Convert.ToDouble(txOperand1.Text);
            double operand2 = Convert.ToDouble(txtOperand2.Text);
            string operation = txtOperator.Text;
            double result = 0;

            // Verify user input is a valid operator. If valid, run getCalculation method and
            // output result to result text box.  If invalid, display error message.
            if (operation == "/" || operation == "*" || operation == "+" || operation == "-")
            {
            result = getCalculation (operand1, operand2, operation);
            txtResult.Text = result.ToString();
            }
            else{
            txtResult.Text = "ERROR";
            lblError.Text = "Please enter a valid operator:\nUse: +   -   /   *";
            }
        }

            //Calulate 2 Operands based on input from user.
             public double getCalculation(double num1, double num2, string sign)
             {
                double answer = 0;

                switch (sign)
                { 
                case "/":
                    answer = num1 / num2;
                    break;
                case "*":
                    answer = num1 * num2;
                    break;
                case "+":
                    answer = num1 + num2;
                    break;
                case "-":
                    answer = num1 - num2;
                    break;
                }

                return answer;
             }

             // Clears Result text box if any new input is typed into the other 3 fields.
             private void txOperand1_TextChanged(object sender, EventArgs e)
             {
                 txtResult.Text = "";
             }

             private void txtOperator_TextChanged(object sender, EventArgs e)
             {
                 txtResult.Text = "";
             }

             private void txtOperand2_TextChanged(object sender, EventArgs e)
             {
                 txtResult.Text = "";
             }

             private void btnExit_Click(object sender, EventArgs e)
             {
                 this.Close();
             }

    }
JLego
  • 17
  • 2
  • Have you looked at this: http://stackoverflow.com/questions/3558814/net-textbox-handling-the-enter-key – David Tansey Aug 18 '13 at 16:31
  • or maybe this one: http://stackoverflow.com/questions/6290967/stop-the-ding-when-pressing-enter?rq=1 – David Tansey Aug 18 '13 at 16:39
  • You haven't mentioned what method you are using to associate the Enter key with the calculate button. Please either explain your strategy or post the designer file for your form. – Paul Keister Aug 18 '13 at 16:42
  • David, Yes I did look at those pages actually. They didn't answer my question. Hans did below. I have not learned anything about key input or anything and wanted to complete my assignment as instructed. My problem was with my Form's properties. All set. Thanks though! – JLego Aug 18 '13 at 19:58

2 Answers2

1

This happens when one of your text boxes has the focus. It ding-a-lings you to remind you that it doesn't know what to do with the Enter key. It only has a meaning if the box' MultiLine property is set to true. Of course it is not set.

The Enter key is special, along with the Escape key, it is intended to operate the default button of the window. You have one, you like your Calculate button to be your default button. So select your form in the designer and change the AcceptButton property, pick your Calculate button from the combobox.

Don't forget to add the code that checks that the text boxes have valid strings in them that can be converted to a number with, say, double.TryParse().

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0
  private void btn_equal_Click(object sender, EventArgs e)
    {
        //this is counter is for the for the count how many time equal button is press 
        intCountEqual++;
        if (strOperators == "")
        {
            if(txt_screen.Text.Contains("0.00000000")||txt_screen.Text.Contains("0.0"))
            {
                txt_screen.Text = "0";
                lbl_history.Text = "0" + "=";
            }
            else
            {
                decValue1 = Convert.ToDecimal(txt_screen.Text);
                txt_screen.Text = Convert.ToString(decValue1);
                lbl_history.Text = txt_screen.Text + "=";
            }
        }
        //If the error is display then nothing will change 
        else if (intCountError > 0)
        {
            btn_clear.PerformClick();
        }
        else
        {
            try
            {
                //this switch case is check the what operation is perform 
                switch (strOperators)
                {
                    case "+":
                        //if the intCountEqual greaterthan 1 then perform if part other wise perform else part 
                        if (intCountEqual > 1)
                        {
                            //if the result is display then press equel so this operation is perfrom 
                            //value2 + result = new result like 10+20 = 30 (Press equal so result is 30+20=50) 
                            lbl_history.Text = Convert.ToString(decResult) + "+" + Convert.ToString(decValue2) + "=";
                            decResult = decValue2 + decResult;
                            txt_screen.Text = Convert.ToString(decResult);
                          
                        }
                        else
                        {
                            //if the result is not display that mean first you press equal button then perfrom this block
                            //value1 + value2 = result that mean 10+20 = 30 
                            decValue2 = Convert.ToDecimal(txt_screen.Text);
                            lbl_history.Text += Convert.ToString(decValue2) + "=";
                            decResult = decValue1 + decValue2;
                        }
                        break;
                    case "-":
                        if (intCountEqual > 1)
                        {
                            lbl_history.Text = Convert.ToString(decResult) + "-" + Convert.ToString(decValue2) + "=";
                            decResult = decResult - decValue2;
                            txt_screen.Text = Convert.ToString(decResult);
                           
                        }
                        else
                        {
                            decValue2 = Convert.ToDecimal(txt_screen.Text);
                            lbl_history.Text += Convert.ToString(decValue2) + "=";
                            decResult = decValue1 - decValue2;
                        }
                        break;
                    case "*":
                        if (intCountEqual > 1)
                        {
                            lbl_history.Text = Convert.ToString(decResult) + "*" + Convert.ToString(decValue2) + "=";
                            decResult = decResult * decValue2;
                            txt_screen.Text = Convert.ToString(decResult);
                        }
                        else
                        {
                            decValue2 = Convert.ToDecimal(txt_screen.Text);
                            lbl_history.Text += Convert.ToString(decValue2) + "=";
                            decResult = decValue1 * decValue2;
                        }
                        break;
                    case "/":
                        if (intCountEqual > 1)
                        {
                            lbl_history.Text = Convert.ToString(decResult) + "/" + Convert.ToString(decValue2) + "=";
                            decResult = decResult / decValue2;
                            txt_screen.Text = Convert.ToString(decResult);
                        }
                        else
                        {
                            decValue2 = Convert.ToDecimal(txt_screen.Text);
                            lbl_history.Text += Convert.ToString(decValue2) + "=";
                            if (decValue2 == 0)
                            {
                                intCountError++;
                                lbl_history.Text = decValue1 + strOperators + decValue2 + "=";
                                txt_screen.Font = new Font(txt_screen.Text, 14, FontStyle.Bold);
                                txt_screen.Text = "Divide by zero is not possible";
                            }
                            else
                            {
                                decResult = decValue1 / decValue2;
                            }
                        }
                        break;
                }

                //First result is generat then change in round 
                decResult = Math.Round(decResult, 4);
                //check the length of our result and store in the result variable 
                int result = length_funtion(Convert.ToString(decResult));
                //If first check the operand_2 and operator is / then generate error 
                if (decValue2 == 0 && strOperators == "/")
                {
                    intCountError++;
                    lbl_history.Text = decValue1 + strOperators + decValue2 + "=";
                    txt_screen.Font = new Font(txt_screen.Text, 14, FontStyle.Bold);
                    txt_screen.Text = "Divide by zero is not possible";
                }
                else
                {
                    //if result length is 9 then display other wise generate error 
                    if (result <= 9)
                    {
                        txt_screen.Text = Convert.ToString(decResult);
                    }
                    else
                    {
                        intCountError++;
                        txt_screen.Text = "Result is out of the range";
                        txt_screen.Font = new Font(txt_screen.Text, 18, FontStyle.Bold);
                        lbl_history.Text = "";
                    }
                }
            }
            catch
            {
                txt_screen.Text = "Invalid Input";
            }
        }
    }
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '22 at 17:00