1

我一直在这个问题上运行,它会跳出生成表的 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

End Function

4

2 回答 2

0

您是否可以在 For 循环中添加断点以查看在什么点生成错误?如果是这样,请在引发异常之前检查局部变量值是什么。

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

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

于 2012-03-29T12:24:31.083 回答
0

forin包括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
于 2012-03-29T12:17:15.570 回答