nitin goyal
nitin goyal

Reputation: 63

Need a little guidance to make GUI calculator using C#?

I'm completely new in WPF but good familiar with oop, generics etc.. But i'm trying to make a calculator, have a look into

SCREENSHOT

where i get my approach by my own coding.

But the problem I'm little confused is how to get that value which is showed into textbox and do sum?

look into source:

using System;
using System.Windows;
using System.Text;

namespace WpfCalculatorGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public void nine(Object sender, RoutedEventArgs e)
        {
            DispBox.AppendText("9");

        }
    }
}

Please help with guidance.

Upvotes: 2

Views: 1279

Answers (2)

Vipul Sharma
Vipul Sharma

Reputation: 11

Set Name="tb"(that's what I named my textbox) and add Click="Button_Click" to your 1,2,3,4,.... buttons then do this in .cs file to display clicked numerics in textbox

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button b = (Button)sender;
        tb.Text += b.Content.ToString();
    }

 private void Result_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                result();
            }
            catch (Exception exc)
            {
                Console.WriteLine("Receiving", exc.StackTrace);
                tb.Text = "Error!";
            }
        }

private void result()
        {
            String op;
            int iOp = 0;
            if (tb.Text.Contains("+"))
            {
                iOp = tb.Text.IndexOf("+");
            }
            else if (tb.Text.Contains("-"))
            {
                iOp = tb.Text.IndexOf("-");
            }
            else if (tb.Text.Contains("*"))
            {
                iOp = tb.Text.IndexOf("*");
            }
            else if (tb.Text.Contains("/"))
            {
                iOp = tb.Text.IndexOf("/");
            }
            else
            {
                tb.Text = "Error";
            }

            op = tb.Text.Substring(iOp, 1);
            double op1 = Convert.ToDouble(tb.Text.Substring(0, iOp));
            double op2 = Convert.ToDouble(tb.Text.Substring(iOp + 1, tb.Text.Length - iOp - 1));

            if (op == "+")
            {
                tb.Text += "=" + (op1 + op2);
            }
            else if (op == "-")
            {
                tb.Text += "=" + (op1 - op2);
            }
            else if (op == "*")
            {
                tb.Text += "=" + (op1 * op2);
            }
            else
            {
                tb.Text += "=" + (op1 / op2);
            }
        }

Upvotes: -1

Spook
Spook

Reputation: 25927

The proper approach to this problem is to create MVVM architecture and use bindings. You have to read a lot more about that, because this is a very long topic.

A halfway quick'n'dirty solution might be to bind to the form itself:

<Window x:Name="rootControl" ...

<TextBox Text="{Binding ElementName=rootControl, Path=Display, Mode=TwoWay}" ...

And

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string display;

    // Implement INotifyPropertyChanged

    // This should be done by Commands, actually
    public void Nine(Object sender, RoutedEventArgs e)
    {
        Display += "9";
    }

    public string Display
    {
        get
        {
            return display;
        }
        set
        {
            display = value;
            if (NotifyPropertyChanged != null)
                NotifyPropertyChanged(this, new PropertyChangedEventArgs("Display");
        }
    }
}

Upvotes: 2

Related Questions