CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2012
    Location
    Dallas, TX
    Posts
    1

    C# Calculator App

    Hi, new to the CG boards. Just got back into coding after awhile of not and need some help on a calculator app I am building for the coding practice. I have most of the buttons working, except the operator buttons (add, subtract etc..) do not calculate. The calculate() function I coded isnt getting the otherValue with the getOtherValue() I coded and i cannot figure out why. I get an exception error that says the input string isnt in the correct format. Below is my code, all help is appreciated if you wish to download the full project i have attached it as a zip.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace Calculator
    {
        public partial class MainWindow : Window
        {
            Operation operation;
    
            String buttonContent;
    
            double currentValue;
    
            double otherValue;
    
            double finalValue;
    
            public enum Operation
            {
                Addition,
                Subtraction,
                Multiplication,
                Division
            }
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void btn0_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = buttonContent + btn0.Content.ToString();
    
                textBox.Text = btn0.Content.ToString() + buttonContent;
            }
    
            private void btn1_Click(object sender, RoutedEventArgs e)
            { 
                buttonContent = buttonContent + btn1.Content.ToString();
    
                textBox.Text = btn1.Content.ToString() + buttonContent;
            }
    
            private void btn2_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = buttonContent + btn2.Content.ToString();
    
                textBox.Text = btn2.Content.ToString();
            }
    
            private void btn3_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = buttonContent + btn3.Content.ToString();
    
                textBox.Text = btn3.Content.ToString();  
            }
    
            private void btn4_Click(object sender, RoutedEventArgs e)
            { 
                buttonContent = buttonContent + btn4.Content.ToString();
    
                textBox.Text = btn4.Content.ToString();
            }
    
            private void btn5_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = buttonContent + btn5.Content.ToString();
    
                textBox.Text = btn5.Content.ToString(); 
            }
    
            private void btn6_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = buttonContent + btn6.Content.ToString();
    
                textBox.Text = btn6.Content.ToString();
            }
    
            private void btn7_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = buttonContent + btn7.Content.ToString();
    
                textBox.Text = btn7.Content.ToString();
            }
    
            private void btn8_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = buttonContent + btn8.Content.ToString();
    
                textBox.Text = btn8.Content.ToString();
            }
    
            private void btn9_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = buttonContent + btn9.Content.ToString();
    
                textBox.Text = btn9.Content.ToString();
            }
    
            private void equalsBtn_Click(object sender, RoutedEventArgs e)
            {
    
                textBox.Clear();
    
                textBox.Text = Convert.ToString(getFinalValue());
           
            }     
    
            private void textBox_TextChanged(object sender, TextChangedEventArgs e)
            {
    
                textBox.Text = buttonContent;
    
            }
    
            private void plusBtn_Click(object sender, RoutedEventArgs e)
            {
    
                operation = Operation.Addition;
    
                calculate();
    
            }
    
            private void multiplyBtn_Click(object sender, RoutedEventArgs e)
            {
    
                operation = Operation.Multiplication;
    
                calculate();
    
            }
    
            private void clearBtn_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = null;
    
                textBox.Clear();
            }
    
            private void divideBtn_Click(object sender, RoutedEventArgs e)
            {
    
                operation = Operation.Division;
    
                calculate();
    
            }
    
            private void minusBtn_Click(object sender, RoutedEventArgs e)
            {
    
                operation = Operation.Subtraction;
    
                calculate();
    
            }
    
            private void decimalBtn_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = buttonContent + decimalBtn.Content.ToString();
    
                textBox.Text = decimalBtn.Content.ToString() + buttonContent;
            }
    
            private void negativeBtn_Click(object sender, RoutedEventArgs e)
            {
                buttonContent = "-" + buttonContent;
    
                textBox.Text = negativeBtn.Content.ToString();
            }
    
            private void squaredBtn_Click(object sender, RoutedEventArgs e)
            {
    
                currentValue = getCurrentValue();
    
                textBox.Clear();
    
                double squared = Math.Pow(currentValue,2);
    
                buttonContent = squared.ToString();
    
                textBox.Text = buttonContent;
            }
    
            private void sqareRootBtn_Click(object sender, RoutedEventArgs e)
            {
    
                currentValue = getCurrentValue();
    
                textBox.Clear();
    
                buttonContent = null;
    
                double squareRoot = Math.Sqrt(currentValue);
    
                buttonContent = squareRoot.ToString();
    
                textBox.Text = buttonContent;
            }
    
            private void cubedBtn_Click(object sender, RoutedEventArgs e)
            {
    
                currentValue = getCurrentValue();
    
                textBox.Clear();
    
                double squared = Math.Pow(currentValue, 3);
    
                buttonContent = squared.ToString();
    
                textBox.Text = buttonContent;
            }
    
            private double getCurrentValue()
            {
    
                currentValue = Convert.ToDouble(textBox.Text);
    
                return currentValue;
    
            }
    
            private double getOtherValue()
            {
    
                if (textBox.Text == null)
                {
    
                    otherValue = getCurrentValue();
    
                }
    
                return otherValue;
    
            }
    
            private double getFinalValue()
            {
    
                return finalValue;
    
            }
    
            private double calculate()
            {
                currentValue = getCurrentValue();
    
                buttonContent = null;
    
                textBox.Clear();
    
                for (double wt = 0; wt <= 10; wt++)
                {
    
                    if (wt == Convert.ToDouble(textBox.Text))
                    {
    
                        otherValue = Convert.ToDouble(textBox.Text);
    
                    }
                   
    
                }
    
                    
                switch (operation)
                {
    
                    case Operation.Addition:
                    { 
    
                      finalValue = currentValue + otherValue;
    
                      break;
    
                    }
    
                    case Operation.Subtraction:
                    {
    
                        finalValue = currentValue - otherValue;
    
                        break;
    
                    }
    
                    case Operation.Division:
                    {
    
                        finalValue = currentValue / otherValue;
    
                        break;
    
                    }
    
                    case Operation.Multiplication:
                    {
    
                        finalValue = currentValue * otherValue;
    
                        break;
    
                    }
    
    
                }
    
                return finalValue;
            }
    
        }
    }
    Attached Files Attached Files

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# Calculator App

    When you put a breakpoint in the button handlers, does the breakpoint get hit when you click the button?

    If not, in the form design view, highlight the button, right click and choose properties. In the properties window, click on the lightning bolt icon (it switches to the event view), check to see if the OnClick handler has been set.

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