none
Chart1 Index out of range RRS feed

  • Question

  • My Code:

            

    Private Sub btn_PlotQuad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_PlotQuad.Click
            Dim x(100) As Double
            Dim y(100) As Double

            Chart1.ChartAreas(0).AxisX.LineWidth = 2
            Chart1.ChartAreas(0).AxisY.LineWidth = 2
            Chart1.ChartAreas(0).AxisX.Maximum = 5
            Chart1.ChartAreas(0).AxisX.Minimum = -5

            Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
            Chart1.Series(0).Color = Color.Blue
            For i = -5 To 5
                For j = 0 To 10
                    x(j) = i
                    y(j) = (a * (i) ^ 2) + (b * i) + c
                    Chart1.Series(0).Points.AddXY(x(j), y(j))
                Next
            Next
        End Sub

    When I run the program, I can input the quadratic equation and plot it on the chart, but when I clear the chart and enter another equation in, it states that the index is out of range and so I have to close the program and run it again to plot a different equation.

    

    Friday, July 11, 2014 7:30 PM

Answers

  • Code to clear chart:

    Private Sub btn_ClearQuad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ClearQuad.Click
    ...

            Chart1.Series.Clear()

        End Sub

    What you've done is remove all the Series from the Chart. So, when you try to do Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line, there is no Series(0).

    What you can do instead in your Sub btn_ClearQuad_Click is to use Chart1.Series(0).Points.Clear()

    HTH,

    Andrew

    • Marked as answer by jpmorris64 Friday, July 11, 2014 9:13 PM
    Friday, July 11, 2014 8:38 PM

All replies

  • Where is you code that is clearing the chart?  You can't keep using the object Chart1.  Everytime you create a new chart the index of the chart increases.  Usually I use ActiveChart to get a new chart object.

    jdweng

    Friday, July 11, 2014 8:14 PM
  • Code to clear chart:

    Private Sub btn_ClearQuad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ClearQuad.Click
            txt_a.Text = ""
            txt_b.Text = ""
            txt_cQuadratic.Text = ""
            txt_root1.Text = ""
            txt_root2.Text = ""
            txt_Numroots.Text = ""
            Chart1.Series.Clear()

        End Sub

    Friday, July 11, 2014 8:24 PM
  • Code to clear chart:

    Private Sub btn_ClearQuad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ClearQuad.Click
    ...

            Chart1.Series.Clear()

        End Sub

    What you've done is remove all the Series from the Chart. So, when you try to do Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line, there is no Series(0).

    What you can do instead in your Sub btn_ClearQuad_Click is to use Chart1.Series(0).Points.Clear()

    HTH,

    Andrew

    • Marked as answer by jpmorris64 Friday, July 11, 2014 9:13 PM
    Friday, July 11, 2014 8:38 PM
  • Thanks, understand it now and it works perfectly.
    Friday, July 11, 2014 9:05 PM