vb.netselectindexingindexoutofrangeexceptiontextchanged

TextChanged and switch statement to judge the changed textbox and rebuild chart data and update (Index out of Range) VB.Net


I am struggling with using the switch statement and textchanged to update a chart in a panel on my form so that when text is changed the chart will rebind with the new values so as to create a dynamic chart in a sense.

My code below keeps returning an index out of range on the Case T8K.Name : Me.yvals(7) = Convert.ToInt32(T8K.Text). (See below)

It may also be important to point out that all text boxes and the chart are located inside a panel

Could anyone please assist with what i may have done wrong.

Public Class Dashboard
    Private xvals As New List(Of Integer)
    Private yvals As New List(Of Integer)

Private Sub Dashboard_Load(Sender As Dashboard, e As EventArgs) Handles MyBase.Load
        'loads data into the 'TCClientDBDataSet.Client_DB' table.
        Me.Client_DBTableAdapter.Fill(Me.TCClientDBDataSet.Client_DB)

        'Chart Setup
        'Dim A5K1 As Integer = T5K1.Text
        'Dim A5K2 As Integer = T5K2.Text
        'Dim A5K3 As Integer = T5K3.Text
        'Dim average = (A5K1 + A5K2 + A5K3) / 3

        Me.xvals = New List(Of Integer) From {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000}
        Me.yvals = New List(Of Integer) From {T1K.Text, T2K.Text, T3K.Text, T4K.Text, T5K1.Text, T6K.Text, T7K.Text, T8K.Text, T9K.Text, T10K.Text}

        Dim xmaxvals() As Integer = ({1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000})
        Dim ymaxvals() As String = {tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text}

        Testpoint_Chart.Series.Clear()
        Testpoint_Chart.Series.Add("Test Data")
        Testpoint_Chart.Series.Add("Max Torque")

        With Testpoint_Chart.ChartAreas(0)
            .AxisX.Title = "Pressure (Psi)"
            .AxisX.Minimum = 1000
            .AxisX.Maximum = 10000
            .AxisX.Interval = 1000

            .AxisY.Interval = 1000
            .AxisY.Title = "Test Points (ft.lb)"

            'If T10K.Text > Math.Ceiling(tst_MaxOutput.Text / 1000) * 1000 Then
            .AxisY.Maximum = Math.Ceiling(T10K.Text / 1000) * 1000
            'Else
            '.AxisY.Maximum = Math.Ceiling(tst_MaxOutput.Text / 1000) * 1000
            'End If
        End With

        'Test Points
        With Testpoint_Chart.Series("Test Data")
            .IsVisibleInLegend = True
            .ChartType = SeriesChartType.Line
            .IsValueShownAsLabel = True
            .Points.DataBindXY(Me.xvals, Me.yvals)
        End With

        'Max Torque
        With Testpoint_Chart.Series("Max Torque")
            .IsVisibleInLegend = True
            .ChartType = SeriesChartType.Line
            .IsValueShownAsLabel = False
            .Color = Color.Red
            .Points.DataBindXY(xmaxvals, ymaxvals)
        End With
    End Sub

    Private Sub TextBox_TextChanged(Sender As TextBox, e As EventArgs) Handles T1K.TextChanged, T2K.TextChanged, T3K.TextChanged, T4K.TextChanged, T5K1.TextChanged, T6K.TextChanged, T7K.TextChanged, T8K.TextChanged, T9K.TextChanged, T10K.TextChanged
        Select Case Sender.Name
            Case T1K.Name : Me.yvals(0) = Convert.ToInt32(T1K.Text)
            Case T2K.Name : Me.yvals(1) = Convert.ToInt32(T2K.Text)
            Case T3K.Name : Me.yvals(2) = Convert.ToInt32(T3K.Text)
            Case T4K.Name : Me.yvals(3) = Convert.ToInt32(T4K.Text)
            Case T5K1.Name : Me.yvals(4) = Convert.ToInt32(T5K1.Text)
            Case T6K.Name : Me.yvals(5) = Convert.ToInt32(T6K.Text)
            Case T7K.Name : Me.yvals(6) = Convert.ToInt32(T7K.Text)
            Case T8K.Name : Me.yvals(7) = Convert.ToInt32(T8K.Text)
            Case T9K.Name : Me.yvals(8) = Convert.ToInt32(T9K.Text)
            Case T10K.Name : Me.yvals(9) = Convert.ToInt32(T10K.Text)
        End Select

        Me.Testpoint_Chart.Series("Test Data").Points.DataBindXY(Me.xvals, Me.yvals)
        Me.Testpoint_Chart.Update()
    End Sub

Solution

  • Thank you greatly @jmcilhinney It turned out to be a conversion issue from String which means my index was empty, so after reworking the code a bit and using NumericUpDown controls with an array i managed to get it working.