Sunday, 18 August 2013

Windows-Ding when I press Return on C# form application

Windows-Ding when I press Return on C# form application

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

No comments:

Post a Comment