在运行我的代码vb.net时不断获得参数OUOfRangeExceptions

时间:2012-03-29 12:05:56

标签: winforms vb.net-2010 tablelayoutpanel

我继续在这个问题中运行,它将跳出生成表的for语句,我已经在代码中倾倒了三个小时更好的部分,我无法找到出错的地方所以我想我需要另一双眼睛。

Shared Function DrawGrid() As TableLayoutPanel
Dim dayNames As New ArrayList
dayNames.Add("Monday")
dayNames.Add("Tuesday")
dayNames.Add("Wednesday")
dayNames.Add("Thursday")
dayNames.Add("Friday")
dayNames.Add("Saturday")
dayNames.Add("Sunday")
Dim hour As Integer = 8
Dim minute As Integer = 0
Dim timeType As String = "AM"
Dim dayLength As Integer = 12
Dim timetable As New TableLayoutPanel
timetable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset

'Loops through days one at a time this creates the labels and adds them for reference by the user but is not needed for the timetable creation
For days As Integer = 0 To 7
    timetable.ColumnCount += 1
    timetable.RowCount += 1
    If days > 0 Then
        Dim NamePos As New TableLayoutPanelCellPosition(days, 0)
        Dim lblDay As New Label

        lblDay.Text = CStr(dayNames.Item(days))

        timetable.SetCellPosition(lblDay, NamePos)
        timetable.Controls.Add(lblDay)
    End If

    For time As Integer = 0 To dayLength
        Dim rowPos As New TableLayoutPanelCellPosition(days, time)
        Dim lblTime As New Label
        Dim timeString As String
        timetable.RowCount += 1
        If days = 0 Then
            minute += 6
            If minute = 6 Then
                minute = 0
                hour += 1

            End If
            If hour = 13 Then
                hour = 1
                timeType = "PM"
            End If
            timeString = "Time is " & hour & ":" & minute & "0 " & timeType
            lblTime.Text = timeString

            timetable.SetCellPosition(lblTime, rowPos)

            timetable.Controls.Add(lblTime)
            timetable.Visible = True

        End If


    Next
Next
timetable.GrowStyle = TableLayoutPanelGrowStyle.AddColumns
timetable.AutoSize = True
MessageBox.Show("Working")
Return timetable

'Loops through days one at a time this creates the labels and adds them for reference by the user but is not needed for the timetable creation For days As Integer = 0 To 7 timetable.ColumnCount += 1 timetable.RowCount += 1 If days > 0 Then Dim NamePos As New TableLayoutPanelCellPosition(days, 0) Dim lblDay As New Label lblDay.Text = CStr(dayNames.Item(days)) timetable.SetCellPosition(lblDay, NamePos) timetable.Controls.Add(lblDay) End If For time As Integer = 0 To dayLength Dim rowPos As New TableLayoutPanelCellPosition(days, time) Dim lblTime As New Label Dim timeString As String timetable.RowCount += 1 If days = 0 Then minute += 6 If minute = 6 Then minute = 0 hour += 1 End If If hour = 13 Then hour = 1 timeType = "PM" End If timeString = "Time is " & hour & ":" & minute & "0 " & timeType lblTime.Text = timeString timetable.SetCellPosition(lblTime, rowPos) timetable.Controls.Add(lblTime) timetable.Visible = True End If Next Next timetable.GrowStyle = TableLayoutPanelGrowStyle.AddColumns timetable.AutoSize = True MessageBox.Show("Working") Return timetable

2 个答案:

答案 0 :(得分:0)

for中的VB.NET是包容性的,例如:

            For i = 0 To 5
                Debug.WriteLine(i)' outputs : 0,1,2,3,4,5
            Next

所以你可能想在循环时使用一些-1

这例如迭代8次,而你想要只迭代7次(除非在你的国家,你一周有8天)

For days As Integer = 0 To 7 

以下是可能的更正:

      For days As Integer = 0 To 6
            timetable.ColumnCount += 1
            timetable.RowCount += 1
            If days > 0 Then
                Dim NamePos As New TableLayoutPanelCellPosition(days, 0)
                Dim lblDay As New Label

                lblDay.Text = CStr(dayNames.Item(days))

                timetable.SetCellPosition(lblDay, NamePos)
                timetable.Controls.Add(lblDay)
            End If

            For time As Integer = 0 To dayLength - 1
                Dim rowPos As New TableLayoutPanelCellPosition(days, time)
                Dim lblTime As New Label
                Dim timeString As String
                timetable.RowCount += 1
                If days = 0 Then
                    Minute += 6
                    If Minute() = 6 Then
                        Minute = 0
                        Hour += 1

                    End If
                    If Hour() = 13 Then
                        Hour = 1
                        timeType = "PM"
                    End If
                    timeString = "Time is " & Hour() & ":" & Minute() & "0 " & timeType
                    lblTime.Text = timeString

                    timetable.SetCellPosition(lblTime, rowPos)

                    timetable.Controls.Add(lblTime)
                    timetable.Visible = True

                End If


            Next
        Next

答案 1 :(得分:0)

您是否可以在For循环中添加断点以查看生成错误的位置?如果是这样,那么在它抛出异常之前检查局部变量值是什么。

超出范围的异常可能是因为您正在尝试访问不存在的数组/集合项。

例如myArray(4)有5个项目,如果我尝试访问myArray(5),我将得到一个超出范围的异常,因为索引从0开始。