|
-
Oct 17th, 2012, 10:39 PM
#1
Thread Starter
Junior Member
I need help with my Code Its driving me CRAZY
well, I am to design a form(a calculator but without buttons when selecting an operator +-*/) that accepts an operand(such as a number) and operator(+-*/) and a second operand(another number) and then gives the result, I then
need to have it show an error message if an invalid operator is enter by the user. My problem is even if the right operator +,*,-,/ is entered it still shows an error
message stating its an invalid???
this has really been driving me crazy i can't seem to figure it out??
I really think its the isOperator method that is causing the issues??
HTML Code:
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
if (IsValidData())
{
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
string Operator1 = Convert.ToString(txtOperator.Text);
decimal result = 0m;
result = Calculate(operand1, Operator1, operand2);
txtResult.Text = result.ToString("n4");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
}
private decimal Calculate(decimal operand1, string Operator1, decimal operand2)
{
if (Operator1 == "+")
{
operand1 += operand2;
}
else if (Operator1 == "-")
{
operand1 -= operand2;
}
else if (Operator1 == "*")
{
operand1 *= operand2;
}
else if (Operator1 == "/")
{
operand1 /= operand2;
}
{
return operand1;
}
}
public bool IsPresent(TextBox textbox, string name)
{
if (textbox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textbox.Focus();
return false;
}
return true;
}
public bool IsDecimal(TextBox textbox, string name)
{
try
{
Convert.ToDecimal(textbox.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(name + " Must be a decimal value.", "Entry Error");
textbox.Focus();
return false;
}
}
public bool IsOperator(TextBox textbox, string name)
{
string validOperator1 = "";
try
{
validOperator1 = Convert.ToString(textbox.Text);
if (validOperator1 != "+" | validOperator1 != "-" | validOperator1 != "*" | validOperator1 != "/")
{
MessageBox.Show(name + " Must be a Valid Operator", "Entry Error");
textbox.Focus();
}
return false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
return true;
}
public bool IsWithinRange(TextBox textbox, string name, decimal min, decimal max)
{
decimal number = Convert.ToDecimal(textbox.Text);
if (number < min || number > max)
{
MessageBox.Show(name + " Must be between " + min + " and " + max + " ", "Entry Error");
textbox.Focus();
return false;
}
return true;
}
public bool IsValidData()
{
return
IsPresent(txtOperand1, "Operand 1") &&
IsDecimal(txtOperand1, "Operand 1") &&
IsWithinRange(txtOperand1, "Operand 1", 1, 999) &&
IsPresent(txtOperator, "Operator") &&
IsOperator(txtOperator, "Operator") &&
IsPresent(txtOperand2, "Operand 2") &&
IsDecimal(txtOperand2, "Operand 2") &&
IsWithinRange(txtOperand2, "Operand 2", 1, 999);
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void ClearResult(object sender, EventArgs e)
{
txtResult.Text = "";
}
}
}
Last edited by QuestionPlease; Oct 17th, 2012 at 10:44 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|