I'm working on a scientific/algebraic calculator and I have put all of the code that will deal with any of the math in a separate class. However I cannot access any of the controls on the form from this class. I need to write the answer into the box on my form called AnswerBox. How do I allow this other class to use controls on the form?

Recommended Answers

All 5 Replies

In your form add a property like so:

public decimal Answer
{
  get
  {
    return Convert.ToDecimal(AnswerBox.Text);
  }
  set
  {
    AnswerBox.Text = value.ToString();
  }
}

Then in your other class add a reference to the form and set the Answer property accordingly.

class Calculator
{
  private MyForm form;
  public Calculator (MyForm aForm)
  {
    form = aForm;
  }
  public void Calculate()
  {
    // work out the answer
    //...
    // now set it in the form
    form.Answer = answer;
  }
}

That's not really how mine is set up and that doesn't seem to be working... I don't think I'm putting them in the right place. Here's what happens in mine. I probably don't have the best way of doing things. :P I'm a beginner to C#.

On the form. Pressing equals button

private void EqualsButton_Click(object sender, EventArgs e)
        {
            MathLogic.Start(EntryBox.Text.Replace(" ", "")); //Strip spaces and send to MathLogic
        }

Beginning of MathLogic class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScientificCalculator
{
    public class MathLogic
    {
        public static string[] Operators = new string[] { "+", "–", "x", "÷" };
        public static string Entry = "";

        public static void Start(string e)
        {
            Entry = e;

            //Determine what to do to the entry. In this case 2 + 2
        }

Adding the numbers and updating the AnswerBox. This is what I want it to do

if (Operator == "+")
{
     try
     {
          int x = numbers[0];
          int y = numbers[1];

          AnswerBox.AppendText(Add(x, y).ToString());
     }

     catch
     {
          AnswerBox.AppendText("SYNTAX ERROR");
     }
}

Can you pass the text box as a parameter to your Start method?

public static void Start(string e, TextBox answerBox)
{
  Entry = e;
  //Determine what to do to the entry. In this case 2 + 2
  // ...
  // update the text box with the answer
  try
  {
    answerBox.Text += Add(x, y).ToString();
  }
  catch
  {
    answerBox.Text += "SYNTAX ERROR";
  }
}

Yes, that works. But is there a way to make the AnswerBox available to other functions in the class without adding it as a parameter to all of the other functions?

Not if all of your methods are static. What I would do is make your methods non-static and pass the form to the constructor of the MathLogic class, then in your form add the property as suggested above and create a MathLogic instance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.