Hello,
I am finally getting down OOP in C#, i just need assistance with one thing on my calculator project (hopefully its just one thing), I have the textbox, which object is called txtbox1, the button part is my big hurdle right now, so far i thought of
But i know its wrong because it is passing it as a string, i would need to pass it as a double, how do i do that, i say this is wrong because when i press the button... only 2 comes up, but if i press it again only 2 comes up, and it does not turn it into 22. Hope this makes sense, also if you can give me a tip/hint or advice on how to do the equation part to since i am a little lost on that. my class is called numbers, which i declared the class to be num if you are wondering.Code:private void btn2_Click(object sender, EventArgs e) { num.numbering = 2; txtbox1.Text = "2"; }
Last edited by Jordan; 10-01-2008 at 01:53 PM.
It sounds like you only need to concatenate the string:
More: How to: Concatenate Multiple Strings (C# Programming Guide)Code:private void btn2_Click(object sender, EventArgs e) { num.numbering = 2; txtbox1.Text += "2"; }
Thanks jordan,
that took care of the problem, i will also look into that link that you attached.
My last question of course would happen to do with the equation part of the calculator, I am a little lost, if anyone can give me an example,tip, hint etc for that it would be great.
What do you mean? And can you post what code you have already?
Sorry for the confusion, so the main thing as I said, i am making a calculator that will help me with my OOP, now what i have made successfully so far is a class called numbers which is below and a form, which has all the number buttons in place which is below the numbers class, what i am thinking that might work is make another class called equations, which I will make methods for + - * /, then... thats all i go so far : ) i am hoping some one can give me the rest what I am missing that can help me out and plus this would go good in the tutorial section. I hope this explains better as well as the class equation is a good idea?
So here is the form programCode:namespace WindowsFormsApplication1 { class numbers { private double number; private double number2; public double numbering { get { return number; } set { number = value; } } public double numbering2 { get { return number2; } set { number2 = value; } } } }
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 WindowsFormsApplication1 { public partial class Form1 : Form { numbers num = new numbers(); public Form1() { InitializeComponent(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void Form1_Load(object sender, EventArgs e) { } private void btnclear_Click(object sender, EventArgs e) { num.numbering = 0; } private void txtbox1_TextChanged(object sender, EventArgs e) { } private void btn1_Click(object sender, EventArgs e) { try { num.numbering = 1; txtbox1.Text += "1"; } catch(Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn2_Click(object sender, EventArgs e) { num.numbering = 2; txtbox1.Text += "2"; } private void btn3_Click(object sender, EventArgs e) { num.numbering = 3; txtbox1.Text += "3"; } private void btn4_Click(object sender, EventArgs e) { num.numbering = 4; txtbox1.Text += "4"; } private void btn5_Click(object sender, EventArgs e) { num.numbering = 5; txtbox1.Text += "5"; } private void btn6_Click(object sender, EventArgs e) { num.numbering = 6; txtbox1.Text += "6"; } private void btn7_Click(object sender, EventArgs e) { num.numbering = 7; txtbox1.Text += "7"; } private void btn8_Click(object sender, EventArgs e) { num.numbering = 8; txtbox1.Text += "8"; } private void btn9_Click(object sender, EventArgs e) { num.numbering = 9; txtbox1.Text += "9"; } private void btn0_Click(object sender, EventArgs e) { num.numbering = 0; txtbox1.Text += "0"; }
Last edited by TcM; 10-02-2008 at 11:57 AM.
my bad.... did the code thing wrong : (
I guess my hurdle i am trying to get over, making a calculator like windows pretty much, using only 1 text box, when a user clicks on button, 1, and shows up number 1 in text box, but getting it to click on a equation and then a number is a mystery to me, like how do they collect data provided by 1 text box? I have the code so far from what i got but i know i am doing something wrong, i hope this helps the confusion.
and here is my two classes which first class is numbers: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 WindowsFormsApplication1 { public partial class Form1 : Form { numbers num = new numbers(); public Form1() { InitializeComponent(); } enum operation { add, subtract, divide, multiply } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void Form1_Load(object sender, EventArgs e) { } private void btnclear_Click(object sender, EventArgs e) { num.numbering = 0; } private void txtbox1_TextChanged(object sender, EventArgs e) { } private bool firstNumberSet = false; private void btn1_Click(object sender, EventArgs e) { try { if (!firstNumberSet) { num.numbering = 1; } else { num.numbering2 = 2; } txtbox1.Text += "1"; } catch(Exception) { MessageBox.Show("WHAT DID YOU DO!!"); } } private void btn2_Click(object sender, EventArgs e) { num.numbering = 2; txtbox1.Text += "2"; } private void btn3_Click(object sender, EventArgs e) { num.numbering = 3; txtbox1.Text += "3"; } private void btn4_Click(object sender, EventArgs e) { num.numbering = 4; txtbox1.Text += "4"; } private void btn5_Click(object sender, EventArgs e) { num.numbering = 5; txtbox1.Text += "5"; } private void btn6_Click(object sender, EventArgs e) { num.numbering = 6; txtbox1.Text += "6"; } private void btn7_Click(object sender, EventArgs e) { num.numbering = 7; txtbox1.Text += "7"; } private void btn8_Click(object sender, EventArgs e) { num.numbering = 8; txtbox1.Text += "8"; } private void btn9_Click(object sender, EventArgs e) { num.numbering = 9; txtbox1.Text += "9"; } private void btn0_Click(object sender, EventArgs e) { num.numbering = 0; txtbox1.Text += "0"; } operation currentOperation; operation currentOperationminus; operation currentOperationmult; operation currentOperationdiv; private void btnplus_Click(object sender, EventArgs e) { currentOperation = operation.add; firstNumberSet = true; // this is where we decide that the first number is done and we should //start on the second one } private void btnsubtract_Click(object sender, EventArgs e) { currentOperationminus = operation.subtract; firstNumberSet = true; } private void btnmultiply_Click(object sender, EventArgs e) { currentOperationmult = operation.multiply; firstNumberSet = true; } private void btndivide_Click(object sender, EventArgs e) { currentOperationmult = operation.divide; firstNumberSet = true; } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { aboutfrm bout = new aboutfrm(); bout.Show(); } private void btnequal_Click(object sender, EventArgs e) { numbers num = new numbers(); Equations equat = new Equations(); switch (currentOperation) { case operation.add: equat.add(num.numbering, num.numbering2); break; case operation.subtract: equat.subtract(num.numbering, num.numbering2); break; case operation.multiply: equat.mulitply(num.numbering, num.numbering2); break; case operation.divide: equat.divide(num.numbering, num.numbering2); break; } } } }
second class equations:Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication1 { class numbers { private double number; private double number2; public double numbering { get { return number; } set { number = number * 10; // this will move the other digits left one number += value; // this puts the new one in the ones column } } public double numbering2 { get { return number2; } set { number2 = number2 * 10; // this will move the other digits left one number2 += value; // this puts the new one in the ones column } } } }
Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication1 { class Equations { public double add(double num1, double num2) { return num1 + num2; } public double subtract(double num1, double num2) { return num1 - num2; } public double mulitply(double num1, double num2) { return num1 * num2; } public double divide(double num1, double num2) { return num1 / num2; } } }
So what you want is let's say the user presses 1, in the textbox 1 is displayed, then the user presses + and then presses 2, in the textbox 2 is displayed, and you want the answer (3) to be displayed in the textbox when the user presses =?
In that case you need a temp variable to store the 1st number and then make it + with the second number.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks