i keep running in this issue where it will jump out of the for statements that generate the table, I’ve been pouring over the code for a better part of three hours and i cannot find what is going wrong so i think i need another pair of eyes.
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
Are you able to add a breakpoint to the For loop to see at what point error is being generated? If so check what the local variable values are just before it throws the exception.
An out of range exception can be because you are trying to access an array/collection item that doesn’t exist.
For instance myArray(4) has 5 items, if I try to access myArray(5) I will get an out of range exception, because the index starts at 0.