Skip to content

Hello and some help for a newb

Derik

Asked Feb 16, 2007, 12:07 AM EST4 answers
Share
LinkedInX
Howdy all, I'm fairly new to C# and am in need of some advice on a project I've started working on. I'm trying to create a functioning calculator (similar to the one that comes with windows) yet only used the add/subtract/multiply/divide and decimal functions. I have the form already designed including the 0-9 buttons, the equal button, a button to clear the label(s) as well as buttons for the functions mentioned above. So far I've declared class level variables for the 0-9 buttons, and each button's click event displays the correct number in a label. Now I'm at the part where I need to code for the add/subtract/multiply/divide/decimal/equal buttons. Should I be creating separate methods for each of these functions? Also, once you click a number button and then you click one of the add/subtract buttons etc., should the first number I entered be stored in an invisible label to be included in a calculation later? I just haven't got down the logic behind doing this yet. PS I am using MS VisualStudio.Net 2003 and I can email a zipped version of the project if anyone wants to take a look at it and add code for me to review. Thanks!

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    What you'll need is a member integer/double variable. When an action button (add, subtract, etc) is pressed, store the currently entered number into that member variable. You'll also probably need to store which action has been selected into another member variable (have a search for enumerated types on the net).

    Then when the Equals button is pressed, you can retrieve the action previously selected (e.g. Add), and then use the member variable previously stored. Retrieve the value from the textBox, and perform the calculation, updating the label.


    I imagine the code behind (for example) the add button should be:

    OnAddButtonClick()
    {
          m_fOriginalNumber = numberLabel.Text;
          m_iAction = 1; // should be an enumerated type for each type of action, I'm using 1 = Add

          // Up to you how you handle the entry of a new number, I'm just clearing the control
          // here to keep this example fairly simple...
          textBox1.Text = "";
    }

    OnEqualButtonClick()
    {
         if (m_iOriginalNumber != 0)
         {
              if (m_iAction == 1) // Add
              {
                  int iTemp = textBox1.Text;
                  int iResult = m_fOriginalNumber + iTemp;
                  numberLabel.Text = iResult.ToString();
                  m_fOriginalNumber = 0;
                  m_iAction = 0; // defaulting to be no action defined...
              }
         }
    }

    Hope this helps a little, let me know if any other questions/problems...
    PS. I've not tried this code in any way, so it may not work, just what I've thought up in my head!
    0
  • 0
    Ok I'll kick that around a bit and see what happens. I'm also trying to incorporate an edit menu option that lets you change the background color of the panel. The form name is calculatorForm. When I'm in the code editor and I type in colorDialog1.Color = calculatorForm.BackColor; colorDialog1.ShowDialog(); calculatorForm.BackColor = colorDialog1.Color; I get an error. Am I choosing the wrong thing to change the color for?
    0
  • 0
    I've just tried it myself, and I appear to be working fine. What error are you getting?

    My code is:

    ColorDialog colorDlg1 = new ColorDialog();

    if (colorDlg1 != null)
    {
        colorDlg1.Color = BackColor;

        if (colorDlg1.ShowDialog() == DialogResult.OK)
        {
            BackColor = colorDlg1.Color;
        }
    }

    0
  • 0
    I figured it out. I changed calculatorForm.BackColor to this.BackColor.
    0