Sign in to follow this  
raidzero

C# Calcuator Help

Recommended Posts

raidzero    112
It's only been a few days since I started learning C#, but it doesn't seem like much progress.
So I'm trying to make a simple Windows Calculator application, but I can't exactly figure it out.
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;


public Calculator()
{
InitializeComponent();
}

private void Addition_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
double answer = variableOne + variableTwo;
}

private void Equals_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
string a = answer.ToString();
FieldOne.Text = a;
}

private void Clear_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
}

private void FieldOne_TextChanged(object sender, EventArgs e)
{
double.TryParse(FieldOne.Text, out variableOne);
double.TryParse(FieldOne.Text, out variableTwo);
}
}
}
[/code]

Any help is much appreciated.

Share this post


Link to post
Share on other sites
DaveMS    187
[quote name='Key' timestamp='1310972931' post='4836675']
It's only been a few days since I started learning C#, but it doesn't seem like much progress.
So I'm trying to make a simple Windows Calculator application, but I can't exactly figure it out.
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;


public Calculator()
{
InitializeComponent();
}

private void Addition_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
double answer = variableOne + variableTwo;
}

private void Equals_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
string a = answer.ToString();
FieldOne.Text = a;
}

private void Clear_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
}

private void FieldOne_TextChanged(object sender, EventArgs e)
{
double.TryParse(FieldOne.Text, out variableOne);
double.TryParse(FieldOne.Text, out variableTwo);
}
}
}
[/code]

Any help is much appreciated.
[/quote]


What problems are you having?

Share this post


Link to post
Share on other sites
yaustar    1022
From the looks of the code, I would say that the OP is having trouble separating/parsing the input values into variableOne and variableTwo from one fieldbox.

OP, consider the actions you do when you type in something like 1 + 2 =

The user types in 1, presses add, types in 2 and presses equal.

Your code changes both variableOne and variableTwo at the same time with the same value which is incorrect.

[code]
private void FieldOne_TextChanged(object sender, EventArgs e)
{
double.TryParse(FieldOne.Text, out variableOne);
double.TryParse(FieldOne.Text, out variableTwo);
}[/code]

Share this post


Link to post
Share on other sites
raidzero    112
So I split up variableOne and variableTwo. But when I attempt to get an answer the result is always 0.
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;

public Calculator()
{
InitializeComponent();
}

private void fieldOne_TextChanged(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
}

private void Addition_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
double.TryParse(fieldOne.Text, out variableTwo);
answer = variableOne + variableTwo;
}

private void Equals_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
string a = answer.ToString();
fieldOne.Text = a;
}

private void Clear_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
}
}
}
[/code]

Share this post


Link to post
Share on other sites
DaveMS    187
[quote][code]
private void Addition_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
double.TryParse(fieldOne.Text, out variableTwo);
answer = variableOne + variableTwo;
}

[/code]
[/quote]

I've not done much winforms work, but looks to me like you are clearing the value in fieldone before you read it into variableTwo.


Second - I believe the TextChanged event is fired whenever the text is changed - either by the user or programmatically. There are many peices of code in your example which change the value of fieldOne - this TextChanged event will be fired after each of these occurences. It would seem that the final time this event is fired, fieldone has a value of 0.

Share this post


Link to post
Share on other sites
DaveMS    187
And to add to my previous post

I'm not too clear on your logic.
When do you expect variableOne to be read from the text box?

The way I would do it:

User insert value into fieldOne.
User clicks addition button
program reads value from fieldOne, puts into variableOne, clears fieldOne
User inserts second value into fieldOne
User clicks Equals button
program reads value from fieldOne, puts into variableTwo, clears fieldOne
program calculates answer
program inserts answer into fieldOne

Share this post


Link to post
Share on other sites
raidzero    112
Thanks Dave, that really helped.
So I got it to calculate correctly, but what if I were to do subtraction?
The way I currently have it setup is that when the user presses equal the two variables are added. How can I make it so that if the addition sign was clicked it does addition and same for subtraction?

-Edit-
So I attempted it myself, but now I'm wondering is there a more efficient way of doing this?
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
bool addition, subtraction, multiplication, division;

public Calculator()
{
InitializeComponent();
}

private void fieldOne_TextChanged(object sender, EventArgs e)
{

}

private void Clear_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
}

private void Addition_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
addition = true;
}

private void Subtraction_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
subtraction = true;
}

private void Multiplication_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
multiplication = true;
}

private void Division_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
division = true;
}

private void Equals_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableTwo);
fieldOne.Clear();
fieldOne.Focus();
if (addition == true)
{
answer = variableOne + variableTwo;
addition = false;
}
if (subtraction == true)
{
answer = variableOne - variableTwo;
subtraction = false;
}
if (multiplication == true)
{
answer = variableOne * variableTwo;
multiplication = false;
}
if (division == true)
{
answer = variableOne / variableTwo;
division = false;
}
string a = answer.ToString();
fieldOne.Text = a;
}
}
}
[/code]

Share this post


Link to post
Share on other sites
DaveMS    187
[quote name='Key' timestamp='1311025105' post='4837049']
Thanks Dave, that really helped.
So I got it to calculate correctly, but what if I were to do subtraction?
The way I currently have it setup is that when the user presses equal the two variables are added. How can I make it so that if the addition sign was clicked it does addition and same for subtraction?

-Edit-
So I attempted it myself, but now I'm wondering is there a more efficient way of doing this?
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
bool addition, subtraction, multiplication, division;

public Calculator()
{
InitializeComponent();
}

private void fieldOne_TextChanged(object sender, EventArgs e)
{

}

private void Clear_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
}

private void Addition_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
addition = true;
}

private void Subtraction_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
subtraction = true;
}

private void Multiplication_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
multiplication = true;
}

private void Division_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
division = true;
}

private void Equals_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableTwo);
fieldOne.Clear();
fieldOne.Focus();
if (addition == true)
{
answer = variableOne + variableTwo;
addition = false;
}
if (subtraction == true)
{
answer = variableOne - variableTwo;
subtraction = false;
}
if (multiplication == true)
{
answer = variableOne * variableTwo;
multiplication = false;
}
if (division == true)
{
answer = variableOne / variableTwo;
division = false;
}
string a = answer.ToString();
fieldOne.Text = a;
}
}
}
[/code]
[/quote]

One thing i've learnt in the few years i'v been programming - there are [i]always[/i] more efficient ways of doing it. The most important thing is that it works.

If you want to try it though - you can use a delegate as a function pointer, ie - the function to be called can be set at runtime.

Code incoming - not tested.

[code]

namespace Calculator
{


public class OperatorFunctions
{

public delegate double OperatorDelegate(double n1, double n2); // here we declare a delegate function, which has a return type of double, and requires 2 doubles as parameters. We will use it to reference one of the functions below.

public static double Addition(double num1, double num2)
{
return num1 + num2;
}

public static double Subtraction(double num1, double num2)
{
return num1 - num2;
}

public static double Multiplication(double num1, double num2)
{
return num1 * num2;
}

public static double Division(double num1, double num2)
{
return num1 / num2;
}
}


public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
OperatorFunctions.OperatorDelegate op; // here is our delegate object. it will hold a reference to one of the operator methods, although we don't know which one yet.

public Calculator()
{
InitializeComponent();
}


private void ClearAndFocus()
{
fieldOne.Clear();
fieldOne.Focus();
}

private void Clear_Click(object sender, EventArgs e)
{
ClearAndFocus();
}

private void Addition_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Addition; // the delegate object op now stores a reference to the addition method
}

private void Subtraction_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Subtraction; // the delegate object op now stores a reference to the subtraction method
}

private void Multiplication_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Multiplication; // the delegate object op now stores a reference to the multiplication method
}

private void Division_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Division; // the delegate object op now stores a reference to the division method
}

private void Equals_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableTwo);
ClearAndFocus();
answer = op(variableOne, variableTwo); // we are now going to invoke the method referenced by this delegate object. But which method is it? Beats me, but we know it takes 2 doubles as parameters, and returns a double.
fieldOne.Text = answer.ToString();
}
}
}

[/code]

Share this post


Link to post
Share on other sites
DaveMS    187
I should also point out in the above code, there is a possible problem - if the user clicks the equals button without choosing an operation to perform - op will be unassigned. But hey, I did warn you it was untested :)

Share this post


Link to post
Share on other sites
raidzero    112
Haha, thanks for the code though :)

I was adding some new button like the actual numbers 0 through 9, and I also renamed some of the button. I also made sure to rename them inside my class file.
After doing this the button aren't doing what they're suppose to, in fact they do nothing at all.

If I add buttons and don't define what they do in my class file will that cause my whole program to break?

Share this post


Link to post
Share on other sites
falcon93    121
[quote name='Key' timestamp='1311221417' post='4838289']
Haha, thanks for the code though :)

I was adding some new button like the actual numbers 0 through 9, and I also renamed some of the button. I also made sure to rename them inside my class file.
After doing this the button aren't doing what they're suppose to, in fact they do nothing at all.

If I add buttons and don't define what they do in my class file will that cause my whole program to break?
[/quote]

I havn't read through all replies, but I assume you use Visual Studio?

In properties window, click on the yellow thunder icon. On the right of the field "Click", check if the method name for your button click is correct. I'm not sure if it will solve your problem, but it's worth checking out.

For your second question; If you add a button on your form without creating a click event method for it, your program will not crash, and it will neither crash if you leave the click event method empety. However, it's pointless to have a button that doesn't do anything, right? [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]

Share this post


Link to post
Share on other sites
raidzero    112
Thank you so much.
I added button for the actual numbers on them, but I wanted to test if the program still worked before proceeding.
But it all works now, so I can move on.
Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this