CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2010
    Posts
    2

    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

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Simple Calculator Problem

    have you checked to make sure the "btnequals_Click" event is actually firing?
    ===============================
    My Blog

  3. #3
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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.

  4. #4
    Join Date
    Dec 2009
    Posts
    22

    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?

  5. #5
    Join Date
    Feb 2010
    Posts
    2

    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...

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Simple Calculator Problem

    Use the debugger and set a breakpoint...

  7. #7
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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...
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured