// Second constructor takes a string equation, string variable and a color
 // This constructor is called by the other constructor to assign properties and instantiates the Evaluator object
 public Function(string equation, string variable, Color colour)
 {
     Equation = equation;
     Variable = variable;
     Colour = colour;
     evaluator = new Evaluator();
 }
        // Method is called when the user hits a key when in the target point textbox on the differentiation tab
        // If the user presses the enter key, update the TargetPoint control in the DifferentiationPlotter object instance
        private void TargetPointTextBoxKeyDown(object sender, KeyEventArgs e)
        {
            // If the enter key has been pressed
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    // Create an Evaluator object to evaluate the text in the target point textbox
                    // This allows the user to enter input such as 'pi' into the textbox
                    var eval = new Evaluator();

                    // Evaluate the expression and get a result
                    var result = (float) eval.Evaluate(TargetPointTextBox.Text);

                    if((result >= -9999F) && (result <= 9999F))
                    {
                        // If the result is valid, assign the TargetPoint of the grapher to the result
                        differentiationPlotter.TargetPoint = (float) result;
                    }
                    else
                    {
                        // Prompt the user if the value is not valid
                        MessageBox.Show("Target Point must be between -9999 and 9999", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception)
                {
                    // Prompt the user if the text entered could not be evaluated into a numerical result
                    MessageBox.Show("Invalid Target Point", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        // Method calculates an iterational equation up to a certain number
        // At first a user specified number is inserted into the equation and a result is calculated
        // For every subsequent iteration, the most recent calculated value is inserted to give a new result
        // Method populates the listview control with the results
        private void CalculateResults()
        {
            // Create a new Function object with the equation entered in the equation textbox to allow the equation
            // to be evaulated
            var function = new Function(EquationTextBox.Text);
            var eval = new Evaluator();

            // Clear the listview component to remove previous results
            ResultsListView.Items.Clear();

            // A variable to hold the current calculated value
            double result;

            // The number of iterations to use
            int numIterations = (int) NumIterationsUpDown.Value - 1;

            // If the number entered in the first iteration textbox is numerical

            try
            {
                // Evaluate the first iteration
                result = eval.Evaluate(FirstIterationTextBox.Text);
            }
            catch(Exception)
            {
                // Prompt the user if there was an error evaluating
                MessageBox.Show("Invalid first iteration", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Add the first item to the listview. The first iteration is just the first value entered by the user
            var item1 = new ListViewItem("1");
            item1.SubItems.Add(result.ToString(CultureInfo.InvariantCulture));

            // Add the first result to the listview
            ResultsListView.Items.Add(item1);

            try
            {
                // Loop through each iteration
                for (int i = 0; i < numIterations; i++)
                {
                    // Calculate the new result by evaluating the function at the current result
                    double newRes = Math.Round(function.Evaluate(result.ToString(CultureInfo.InvariantCulture)), 5);

                    // Add the new result to the listview control
                    var item = new ListViewItem((i + 2).ToString(CultureInfo.InvariantCulture));
                    item.SubItems.Add(newRes.ToString(CultureInfo.InvariantCulture));
                    ResultsListView.Items.Add(item);

                    // Set the current result to the newly calculated result to get ready for the next iteration
                    result = newRes;
                }
            }
            catch (Exception)
            {
                // Prompt user if there were errors evaluating the function
                MessageBox.Show("Invalid Equation", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        // Method is called when the user presses a key in any of the integration parameter textboxes
        // If the enter key has been pressed, update the properties in the AreaPlotter instance with the new parameters
        private void AreaParamsChange(object sender, KeyEventArgs e)
        {
            // If the enter key ahs been pressed
            if (e.KeyCode == Keys.Return)
            {
                try
                {
                    // Create an Evaluator object to evaluate the text in each parameter textbox
                    // This allows the user to enter input such as 'pi' into a textbox
                    var eval = new Evaluator();

                    // Update the properties in the AreaPlotter with the evaluated results
                    areaPlotter.Lower = (float) eval.Evaluate(LowerLimitTextBox.Text);
                    areaPlotter.Upper = (float) eval.Evaluate(UpperLimitTextBox.Text);
                    var t = (int) eval.Evaluate(IterationsTextBox.Text);

                    // Bounds check the number of iterations to prevent performance issues
                    if (t > 99)
                        throw new Exception("Iterations must be less than 99");

                    if(t <= 0)
                        throw new Exception("Number of iterations must be greater than zero");

                    // If the current graph is a SimpsonPlotter, then the number of iterations cannot be odd
                    if (t % 2 != 0 && areaPlotter is SimpsonPlotter)
                        throw new Exception("Must have even number of strips");

                    areaPlotter.Iterations = (int) eval.Evaluate(IterationsTextBox.Text);

                    e.Handled = true;
                }
                catch (Exception exc)
                {
                    // Prompt the user if any parameter inserted could not be evaluated into a numerical result
                    MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }