How can i draw a tableLayoutPanel on load?

alx212

New member
Joined
Oct 30, 2014
Messages
3
Programming Experience
Beginner
Hello

I'm trying to make a simple timetable using the following function:

VB.NET:
[COLOR=blue][FONT=Consolas]Shared[/FONT][/COLOR][COLOR=blue][FONT=Consolas]Function[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] DrawGrid() [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]TableLayoutPanel[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
 
       [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] dayNames [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]New[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]ArrayList[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
 
       dayNames.Add([/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"Monday"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000])
 
       dayNames.Add([/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"Tuesday"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000])
 
       dayNames.Add([/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"Wednesday"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000])
 
       dayNames.Add([/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"Thursday"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000])
 
       dayNames.Add([/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"Friday"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000])
 
       dayNames.Add([/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"Saturday"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000])
 
       dayNames.Add([/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"Sunday"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000])
 
       [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] hour [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]Integer[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] = 8
 
       [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] minute [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]Integer[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] = 0
 
       [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] timeType [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]String[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] = [/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"AM"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
 
       [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] dayLength [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]Integer[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] = 12
 
       [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] timetable [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]New[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]TableLayoutPanel[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
       timetable.Enabled = [/COLOR][/FONT][COLOR=blue][FONT=Consolas]True[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
       timetable.CellBorderStyle = [/COLOR][/FONT][COLOR=#2B91AF][FONT=Consolas]TableLayoutPanelCellBorderStyle[/FONT][/COLOR][FONT=Consolas][COLOR=#000000].InsetDouble
       timetable.BringToFront()
       [/COLOR][/FONT][COLOR=green][FONT=Consolas]'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[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
       [/COLOR][/FONT][COLOR=blue][FONT=Consolas]For[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] days [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]Integer[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] = 0 [/COLOR][/FONT][COLOR=blue][FONT=Consolas]To[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] 6
           timetable.ColumnCount += 1
           timetable.RowCount += 1
           [/COLOR][/FONT][COLOR=blue][FONT=Consolas]If[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] days > 0 [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Then[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
               [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] NamePos [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]New[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]TableLayoutPanelCellPosition[/FONT][/COLOR][FONT=Consolas][COLOR=#000000](days, 0)
               [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] lblDay [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]New[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]Label[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
               lblDay.Visible = [/COLOR][/FONT][COLOR=blue][FONT=Consolas]True[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
 
               lblDay.Text = [/COLOR][/FONT][COLOR=blue][FONT=Consolas]CStr[/FONT][/COLOR][FONT=Consolas][COLOR=#000000](dayNames.Item(days))
 
               timetable.SetCellPosition(lblDay, NamePos)
               timetable.Controls.Add(lblDay)
           [/COLOR][/FONT][COLOR=blue][FONT=Consolas]End[/FONT][/COLOR][COLOR=blue][FONT=Consolas]If[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
 
           [/COLOR][/FONT][COLOR=blue][FONT=Consolas]For[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] time [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]Integer[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] = 0 [/COLOR][/FONT][COLOR=blue][FONT=Consolas]To[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] dayLength
               [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] rowPos [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]New[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]TableLayoutPanelCellPosition[/FONT][/COLOR][FONT=Consolas][COLOR=#000000](days, time)
               [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] lblTime [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]New[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]Label[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
               [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] timeString [/COLOR][/FONT][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]String[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
               timetable.RowCount += 1
               [/COLOR][/FONT][COLOR=blue][FONT=Consolas]If[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] days = 0 [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Then[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
                   minute += 6
                   [/COLOR][/FONT][COLOR=blue][FONT=Consolas]If[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] minute = 6 [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Then[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
                       minute = 0
                       hour += 1
 
                   [/COLOR][/FONT][COLOR=blue][FONT=Consolas]End[/FONT][/COLOR][COLOR=blue][FONT=Consolas]If[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
                   [/COLOR][/FONT][COLOR=blue][FONT=Consolas]If[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] hour = 13 [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Then[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
                       hour = 1
                       timeType = [/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"PM"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
                   [/COLOR][/FONT][COLOR=blue][FONT=Consolas]End[/FONT][/COLOR][COLOR=blue][FONT=Consolas]If[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
                   timeString = [/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"Time is "[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] & hour & [/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]":"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] & minute & [/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"0 "[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] & timeType
                   lblTime.Text = timeString
 
                   timetable.SetCellPosition(lblTime, rowPos)
 
                   timetable.Controls.Add(lblTime)
                   timetable.Visible = [/COLOR][/FONT][COLOR=blue][FONT=Consolas]True[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
 
               [/COLOR][/FONT][COLOR=blue][FONT=Consolas]End[/FONT][/COLOR][COLOR=blue][FONT=Consolas]If[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
 
 
           [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Next[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
       [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Next[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
       timetable.GrowStyle = [/COLOR][/FONT][COLOR=#2B91AF][FONT=Consolas]TableLayoutPanelGrowStyle[/FONT][/COLOR][FONT=Consolas][COLOR=#000000].AddColumns
       timetable.AutoSize = [/COLOR][/FONT][COLOR=blue][FONT=Consolas]True[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
       timetable.Visible = [/COLOR][/FONT][COLOR=blue][FONT=Consolas]True[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
       timetable.Enabled = [/COLOR][/FONT][COLOR=blue][FONT=Consolas]True[/FONT][/COLOR][FONT=Consolas][COLOR=#000000]
       timetable.CellBorderStyle = [/COLOR][/FONT][COLOR=#2B91AF][FONT=Consolas]TableLayoutPanelCellBorderStyle[/FONT][/COLOR][FONT=Consolas][COLOR=#000000].Inset
       [/COLOR][/FONT][COLOR=#2B91AF][FONT=Consolas]MessageBox[/FONT][/COLOR][FONT=Consolas][COLOR=#000000].Show([/COLOR][/FONT][COLOR=#A31515][FONT=Consolas]"Working"[/FONT][/COLOR][FONT=Consolas][COLOR=#000000])
       [/COLOR][/FONT][COLOR=blue][FONT=Consolas]Return[/FONT][/COLOR][FONT=Consolas][COLOR=#000000] timetable
   [/COLOR][/FONT][COLOR=blue][FONT=Consolas]End[/FONT][/COLOR][COLOR=blue][FONT=Consolas]Function


[/FONT][/COLOR]
But when i try to run in on load, i can see that the function ran with no errors yet no tablelayoutPanel is visible on my form.

Thanks in advance.
 
Where are you adding that TLP to a form? If you don't add it to a form then where are you expecting it to be displayed?

Why would you be creating a TLP in code anyway? Why would you not add it in the designer?
 
because i want to use that function as a template so i wouldn't have to build it all over again.

how can i add the TLP function to the form?

I've tried to call the function on load, plus to draw a TLP on the form and call the function (something like: TLP=DrawGrid())


thanks in advance and sorry for my English.
 
your code said:
timetable.Controls.Add(lblTime)
That is also how you add controls to a form, eg DrawGrid function returns a control: Me.Controls.Add(DrawGrid)
 
Back
Top