// Method is called when the current tab selected is changed
        // Depending on which tab is selected, method removes and adds the corresponding grapher instance
        // This method demonstrates the dynamic creation of user controls
        private void TabControlSelectedIndexChanged(object sender, EventArgs e)
        {
            // If the Integration tab is selected
            if (TabControl.SelectedIndex == 0)
            {
                // Remove the current AreaPlotter instance from the TabControl
                TabIntegration.Controls.Remove(areaPlotter);

                // Create a new default Mid Ordinate Plotter instance with default properties
                areaPlotter = new MidOrdPlotter();
                areaPlotter.ResultChange += AreaPlotterResultChange;
                areaPlotter.Size = new Size(605, 500);
                areaPlotter.Lower = 0;
                areaPlotter.Upper = 10;

                areaPlotter.Equation = EquationTextBox.Text;
                areaPlotter.Dock = DockStyle.Fill;
                areaPlotter.Resize += PlotterResize;
                areaPlotter.PositionMove += PlotterPositionMove;

                // Update the parameter controls on the form to show changes
                UpperLimitTextBox.Text = areaPlotter.Upper.ToString(CultureInfo.InvariantCulture);
                LowerLimitTextBox.Text = areaPlotter.Lower.ToString(CultureInfo.InvariantCulture);
                IterationsTextBox.Text = areaPlotter.Iterations.ToString(CultureInfo.InvariantCulture);

                // Add the new AreaPlotter to the TabControl
                TabIntegration.Controls.Add(areaPlotter);

                // Focus the new AreaPlotter control
                areaPlotter.Focus();
            }
                // If the Differentiation tab is selected
            else if (TabControl.SelectedIndex == 1)
            {
                // Remove the current DifferentiationPlotter instance from the TabControl
                TabDifferentiation.Controls.Remove(differentiationPlotter);

                // Create a new default DifferentiationPlotter instance with default properties
                differentiationPlotter = new DifferentiationPlotter();
                differentiationPlotter.ResultChange += DifferentiationPlotterResultChange;
                differentiationPlotter.Size = new Size(605, 527);

                differentiationPlotter.Equation = EquationTextBox.Text;
                differentiationPlotter.Dock = DockStyle.Fill;
                differentiationPlotter.Resize += PlotterResize;
                differentiationPlotter.PositionMove += PlotterPositionMove;

                // Add the new DifferentiationPlotter to the TabControl
                TabDifferentiation.Controls.Add(differentiationPlotter);

                // Focus the new DifferentiationPlotter control
                differentiationPlotter.Focus();
            }
                // If the Extrema tab is selected
            else if (TabControl.SelectedIndex == 2)
            {
                // Remove the current ExtremaPlotter instance from the TabControl
                TabExtrema.Controls.Remove(extremaPlotter);

                // Create a new default ExtremaPlotter instance with default properties
                extremaPlotter = new ExtremaPlotter();

                extremaPlotter.Dock = DockStyle.Fill;
                extremaPlotter.Size = new Size(605, 527);
                extremaPlotter.Equation = EquationTextBox.Text;

                extremaPlotter.Resize += PlotterResize;
                extremaPlotter.PositionMove += PlotterPositionMove;

                // Add the new ExtremaPlotter to the TabControl
                TabExtrema.Controls.Add(extremaPlotter);

                // Focus the new ExtremaPlotter control
                extremaPlotter.Focus();
            }
                // If the Iterations tab is selected
            else if (TabControl.SelectedIndex == 3)
            {
                // Calculate a new results set and populate the listview control
                CalculateResults();
            }
        }
        // Method is called whenever the user presses a radio button to change the method of integration
        // Method removes the current grapher from the tab and adds a corresponding AreaPlotter depending on which
        // rule of integration has been chosen
        // This method demonstrates the dynamic creation of user controls
        private void AreaGrapherChange(object sender, EventArgs e)
        {
            // Cast the sender object to a RadioButton object to determine which radiobutton was pressed
            var button = sender as RadioButton;

            // If the sender was not a RadioButton instance, exit the method
            if (button == null) return;

            // Get the properties of the current AreaPlotter instance and store in variables
            float x = areaPlotter.OriginX;
            float y = areaPlotter.OriginY;
            int zoom = areaPlotter.Zoom;

            float low = areaPlotter.Lower;
            float upper = areaPlotter.Upper;
            int iter = areaPlotter.Iterations;

            // Remove the current AreaPlotter instance from the TabControl
            TabIntegration.Controls.Remove(areaPlotter);

            // Dispose of the AreaPlotter instance to free resources
            areaPlotter.Dispose();

            // If the Mid Ordinate rule RadioButton was pressed
            if (button.Name.Equals("MidOrdRuleRadioButton"))
            {
                // Set the AreaPlotter instance to become a new Mid Ordinate Plotter
                areaPlotter = new MidOrdPlotter();
            }
                // If the Trapezium rule RadioButton was pressed
            else if (button.Name.Equals("TrapeziumRuleRadioButton"))
            {
                // Set the AreaPlotter instance to become a new Trapezium Plotter
                areaPlotter = new TrapeziumPlotter();
            }
                // If the Simpsons rule RadioButton was pressed
            else
            {
                // Set the AreaPlotter instance to become a new Simpsons Plotter
                areaPlotter = new SimpsonPlotter();
            }

            // Add event handlers to the new AreaPlotter
            areaPlotter.ResultChange += AreaPlotterResultChange;
            areaPlotter.Resize += PlotterResize;
            areaPlotter.PositionMove += PlotterPositionMove;

            // Set the default size of the AreaPlotter
            areaPlotter.Size = new Size(605, 500);

            // Add the new AreaPlotter to the TabControl
            TabIntegration.Controls.Add(areaPlotter);

            // Assign the properties of the new AreaPlotter instance to that of the old AreaPlotter
            areaPlotter.OriginX = x;
            areaPlotter.OriginY = y;
            areaPlotter.Zoom = zoom;

            areaPlotter.Lower = low;
            areaPlotter.Upper = upper;
            areaPlotter.Iterations = iter;
            areaPlotter.Equation = EquationTextBox.Text;
            areaPlotter.Dock = DockStyle.Fill;

            // Repaint the AreaPlotter to show changes
            areaPlotter.Invalidate();

            // Update the parameters and result label
            AreaParamsChange(null, new KeyEventArgs(Keys.Return));
            IntegrationResultLabel.Text = Math.Round(areaPlotter.Result, 4).ToString(CultureInfo.InvariantCulture);
        }