当我在C#窗体应用程序上按Return时,Windows Ding

本文关键字:Return Ding Windows 程序上 应用程序 窗体 应用 | 更新日期: 2023-09-27 18:28:01

我必须为我的C#类制作这个基于方法的计算器。我使用的是VisualStudio2010,任务的一部分是在用户按下回车键时让"Calculate"按钮方法运行。

我以前在VS中也这样做过,但由于某种原因,这次当我尝试输入键时,它一直在响。

这是我的密码。我不认为这与代码有任何关系,但我不知道问题是什么,所以我提出它只是以防万一。

非常感谢您的帮助。非常感谢。

{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();
             }
    }

当我在C#窗体应用程序上按Return时,Windows Ding

当您的一个文本框具有焦点时,就会发生这种情况。它提醒你,它不知道如何使用回车键。只有当框的MultiLine属性设置为true时,它才有意义。当然,它还没有设定。

Enter键是特殊的,与Escape键一起用于操作窗口的默认按钮。你有一个,你喜欢你的计算按钮作为你的默认按钮。因此,在设计器中选择表单并更改AcceptButton属性,从组合框中选择"计算"按钮。

不要忘记添加代码,以检查文本框中是否有可以转换为数字的有效字符串,例如double。TryPasse()。

  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";
            }
        }
    }