+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 12

Thread: calc project

  1. #1
    Programming Professional Siten0308 will become famous soon enough Siten0308's Avatar
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    287

    Question calc project

    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

    Code:
    private void btn2_Click(object sender, EventArgs e)
            {
                num.numbering = 2;
                txtbox1.Text = "2";
            }
    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.
    Last edited by Jordan; 10-01-2008 at 03:53 PM.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: calc project

    It sounds like you only need to concatenate the string:

    Code:
    private void btn2_Click(object sender, EventArgs e)
            {
                num.numbering = 2;
                txtbox1.Text += "2";
            }
    More: How to: Concatenate Multiple Strings (C# Programming Guide)

  3. #3
    Programming Professional Siten0308 will become famous soon enough Siten0308's Avatar
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    287

    Talking Re: calc project

    Thanks jordan,

    that took care of the problem, i will also look into that link that you attached.

  4. #4
    Programming Professional Siten0308 will become famous soon enough Siten0308's Avatar
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    287

    Re: calc project

    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.

  5. #5
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: calc project

    What do you mean? And can you post what code you have already?

  6. #6
    Programming Professional Siten0308 will become famous soon enough Siten0308's Avatar
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    287

    Question Re: calc project

    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?

    Code:
    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; }
            }
        }
    }
    So here is the form program
    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 01:57 PM.

  7. #7
    Programming Professional Siten0308 will become famous soon enough Siten0308's Avatar
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    287

    Re: calc project

    my bad.... did the code thing wrong : (

  8. #8
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: calc project

    Code Tags fixed.
    I still don't understand your problem.

  9. #9
    Programming Professional Siten0308 will become famous soon enough Siten0308's Avatar
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    287

    Question Re: calc project

    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.

    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;
                }
                 
            }
        }
    }
    and here is my two classes which first class is numbers:

    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
                }
            }
        }
    }
    second class equations:

    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;
            }
        }
    }

  10. #10
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: calc project

    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.

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 8
    Last Post: 05-30-2009, 11:56 AM
  2. A cool project idea.
    By AlexanderRybak in forum Community Projects
    Replies: 4
    Last Post: 12-27-2008, 04:14 AM
  3. Send image one project to another ?
    By nomanforu in forum C# Programming
    Replies: 0
    Last Post: 06-03-2008, 12:12 AM
  4. Community Project! - Recent project gone sour
    By Crane in forum C# Programming
    Replies: 5
    Last Post: 09-09-2006, 02:13 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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