I am interested in knowing the quickest way to dynamically add objects. I currently have an example where i need to dynamically create a grid using panels and textboxes. It is comprised of,
one large panel, which is added at design time.
At runtime I add up to 256 panels (16x16) with each panel containing 3 textboxes. To do this I do,
Code:
sub LoadGrid()
For c As Integer = 1 To 17
For r As Integer = 1 To 17
Dim pnlUnicodeCell As New Panel
With pnlUnicodeCell
.Name = "pnlUnicodeCell_" & c & "_" & r
Dim columnX3 As String = "ColumnX:" & c
Dim rowY3 As String = "RowY:" & r
Dim x As Integer = cols3.Item(columnX3) '//cols3 is a hashtable containing x,y points for each cell.
Dim y As Integer = cols3.Item(rowY3)
.Location = New Point(x, y)
If c = 1 And r = 1 Then
.Width = 20
.Height = 20
.BackColor = Color.SeaGreen
ElseIf c = 1 And r <> 1 Then
.Width = 20
.Height = 60
.BackColor = Color.SeaGreen
'Dim cellTxt As New Label 'TextBox
ElseIf r = 1 And c <> 1 Then
.Width = 60
.Height = 20
.BackColor = Color.SeaGreen
Else
.Width = 60
.Height = 60
.BackColor = Color.LightSeaGreen
End If
.BorderStyle = BorderStyle.FixedSingle
End With
pnlUnicode.Controls.Add(pnlUnicodeCell)
Next
Next
AddCells()
End Sub
Public Sub AddCells()
For Each pnlUnicodeCell As Panel In pnlUnicode.Controls
Dim pnlUnicodeCellName As String = pnlUnicodeCell.Name
For c As Integer = 1 To 17
For r As Integer = 1 To 17
Dim curPnlName As String = "pnlUnicodeCell_" & c & "_" & r
If curPnlName = pnlUnicodeCellName Then
If c <> 1 And r <> 1 Then
For g As Integer = 1 To 3
Dim x As Integer = 0
Dim y As Integer
Dim cellTxt As New Label ' TextBox
With cellTxt
Select Case g
Case 1
y = 0
.Height = 30
.Font = New System.Drawing.Font("Microsoft Sans Serif", 20.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Case 2
y = 30
.Height = 15
.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Case 3
y = 45
.Height = 15
.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
End Select
.Location = New Point(x, y)
.Height = 20
.Width = 60
.BorderStyle = BorderStyle.None
.Name = pnlUnicodeCell.Name & "_cell_" & g
.TabIndex = tabNo
.TextAlign = HorizontalAlignment.Center
tabNo += 1
.BackColor = pnlUnicodeCell.BackColor
End With
pnlUnicodeCell.Controls.Add(cellTxt)
cellTxt.BringToFront()
AddHandler cellTxt.Click, AddressOf ClickUnicodeCell
Next
End If
End If
Next
Next
Next
End Sub
This loads really slowly and I would be very grateful if someone could show me a quicker way to do this. Thanks