C# クラス用にこのメソッド ベースの電卓を作成する必要があります。私はVisual Studio 2010を使用していますが、割り当ての一部は、ユーザーがEnterキーを押したときに「計算」ボタンメソッドを実行することです。
以前に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();
}
}