Simple Calculator Problem
Hi guys I'm new to this forum.... :)
I've been working with my simple calculator for almost 2 days now... I am familiar with VB.NET codes and tried to relate it with C# codes... Here is my code:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace prjLabExer5
{
public partial class frmLabExer5 : Form
{
public frmLabExer5()
{
InitializeComponent();
}
string operand;
double Value1 = 0;
double Value2 = 0;
//Double memory;
double ValueT = 0;
private void btn1_Click(object sender, EventArgs e)
{
txtValue.Text += "1";
}
private void btn2_Click(object sender, EventArgs e)
{
txtValue.Text += "2";
}
private void btn3_Click(object sender, EventArgs e)
{
txtValue.Text += "3";
}
private void btn4_Click(object sender, EventArgs e)
{
txtValue.Text += "4";
}
private void btn5_Click(object sender, EventArgs e)
{
txtValue.Text += "5";
}
private void btn6_Click(object sender, EventArgs e)
{
txtValue.Text += "6";
}
private void btn7_Click(object sender, EventArgs e)
{
txtValue.Text += "7";
}
private void btn8_Click(object sender, EventArgs e)
{
txtValue.Text += "8";
}
private void btn9_Click(object sender, EventArgs e)
{
txtValue.Text += "9";
}
private void btndot_Click(object sender, EventArgs e)
{
txtValue.Text += ".";
}
private void btnplus_Click(object sender, EventArgs e)
{
operand = "+";
Value1 = Convert.ToDouble(txtValue.Text);
txtValue.Clear();
}
private void btnminus_Click(object sender, EventArgs e)
{
operand = "-";
Value1 = Convert.ToDouble(txtValue.Text);
txtValue.Clear();
}
private void btnmulti_Click(object sender, EventArgs e)
{
operand = "*";
Value1 = Convert.ToDouble(txtValue.Text);
txtValue.Clear();
}
private void btndivide_Click(object sender, EventArgs e)
{
operand = "/";
Value1 = Convert.ToDouble(txtValue.Text);
txtValue.Clear();
}
private void btnequals_Click(object sender, EventArgs e)
{
Value2 = Convert.ToDouble(txtValue.Text);
switch (operand)
{
case "+":
ValueT = Value1 + Value2;
txtValue.Text = ValueT.ToString() ;
break;
case "-":
ValueT = Value1 - Value2;
txtValue.Text = ValueT.ToString();
break;0
case "*":
ValueT = Value1 * Value2;
txtValue.Text = ValueT.ToString();
break;
case "/":
if (Value2 == 0)
{
txtValue.Text = "Infinity";
}
else if (Value2 != 0)
{
ValueT = Value1 / Value2;
txtValue.Text = ValueT.ToString();
}
break;
}
}
}
}
This program cannot display the result on the textbox... I dont know what I'm missing... please help me guys.... Thanks
Re: Simple Calculator Problem
have you checked to make sure the "btnequals_Click" event is actually firing?
Re: Simple Calculator Problem
Yes. On the surface the code looks good. You have to put a few break points and debug the application. Maybe the "operand" doens't have the right value or as the previous poster suggested the handler is not being called.
Re: Simple Calculator Problem
I'd initialize the operand at the top, instead of just declaring it:
string operand = "+";
That way you'd know if it was ever assigned something else. But now that i think about it you'd prolly get an exception if it isn't initialized when you switch on it.
Yeah, use the debugger, but some breakpoint here and there inside btnequals_Click, to see if it getting called at all. The event might have been detached from the button somehow?
Re: Simple Calculator Problem
hmm.. I dont know how to check wether the btnequals is being called or not... been working but still no go...
Re: Simple Calculator Problem
Use the debugger and set a breakpoint...
Re: Simple Calculator Problem
Incidentally, you could make all of your buttons use the same event handler and do:
Code:
Button btn = sender as Button;
txtValue.Text += btn.Text;
And similar for the operand (although I think the correct term is 'operator') buttons...
May save you a bit of typ-o-ing...