Ich versuche, Bilder dynamisch zu einem TableLayoutPanel hinzuzufügen. Das Hinzufügen scheint zu funktionieren, wenn ich den Inhalt des Controls betrachte, werden die neuen Elemente hinzugefügt. Der RowCount = 10, der IEnnumerable hat 18 Elemente, die die Steuerelemente enthalten, die ich hinzugefügt habe.TableLayoutPanel hinzufügen Steuerelemente nicht sichtbar
Mein Problem ist, dass wenn das Formular angezeigt wird, nur die erste Zeile sichtbar ist. Ich kann die Visible-Eigenschaft nicht dynamisch auf irgendeinen Wert setzen, der Befehl wird ignoriert. Was vermisse ich?
Code:
Private Sub LoadTable()
' get the width in pixels of the columns
Dim oSizeType As SizeType = TableLayoutPanel1.ColumnStyles(0).SizeType
Dim Width As Single
Select Case oSizeType
Case SizeType.Percent, SizeType.AutoSize
Width = TableLayoutPanel1.Width/TableLayoutPanel1.ColumnCount
Case SizeType.Absolute
Width = TableLayoutPanel1.ColumnStyles(0).Width
End Select
' Fix the height of the rows
Dim Height As Single = Width
Dim oPicture As New PictureBox
Dim Cols As Integer = 1
Dim Rows As Integer = -1
Dim Cell As String = String.Empty
' loop through all the images from the folder
For i As Integer = 0 To m_FileList.Count - 1
' establish the current row/column in the table
Dim j As Integer = i + 1
'MsgBox(Fix(j/2))
If Fix(j/2) <> CDbl(j/2) Then
Cols = 0
Rows += 1
' add a row if we have moved to the next row
If Rows > 0 Then TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, Height))
' this doesn't happen automatically
TableLayoutPanel1.RowCount = Rows + 1
Else
Cols = 1
End If
' this is used for the name of some controls
Cell = "R" & Rows & "C" & Cols
' now scale the image to fit into the Table Cells
Dim oPictureBox As New PictureBox
oPictureBox.Height = Height
oPictureBox.Width = Width
Dim oImage As Bitmap = New Bitmap(Bitmap.FromFile(CType(m_FileList.Item(i).Value, String)))
' set the PictureBox properties
oPictureBox.BackgroundImageLayout = ImageLayout.Stretch
oPictureBox.BorderStyle = BorderStyle.Fixed3D
' scale the image to the PictureBox size
'Dim oResized As Image = New Bitmap(Bitmap.FromFile(CType(oPictureBox.Tag, String)))
'ImageEdit.ResizeImage(oImage, oResized, picEnlargement.Width, picEnlargement.Height, False)
ScaleImage(oPictureBox, oImage)
' set the Image of the PictureBox
oPictureBox.Image = oImage
oPictureBox.Name = "PictureBox" & i
' get the path of the current file
Dim f As String = m_FileList(i).Value
'set the properties of the new controls
Dim t As String = GetTitle(f)
'oLabel.Text = t
'oLabel.AutoSize = True
'oLabel.Location = New System.Drawing.Point(30, 110)
oPicture = New PictureBox
With oPicture
SetToolTip(oPicture, f)
oPicture.Tag = f
.Image = oPictureBox.Image
.Dock = DockStyle.Fill
.Size = New System.Drawing.Size(100, 100)
.SizeMode = PictureBoxSizeMode.StretchImage
.Location = New System.Drawing.Point(2, 2)
.Cursor = Cursors.Hand
.Name = "PictureBox" & i + 1
.Visible = True
End With
'here we add the controls to a layout panel to
'manage the positioning of the controls
Dim oContainer As New Panel
With oContainer
.Dock = DockStyle.Fill
.Margin = New System.Windows.Forms.Padding(0)
.Controls.Add(oPicture)
'.Controls.Add(oLabel)
.MaximumSize = New Size(Height, Width)
.MinimumSize = New Size(Height, Width)
.Name = "Container_" & Cell
.Visible = True
End With
' add the
TableLayoutPanel1.Controls.Add(oContainer, Cols, Rows)
'TableLayoutPanel1.SetRow(oContainer, Rows)
'TableLayoutPanel1.SetColumn(oContainer, Cols)
TableLayoutPanel1.Controls.Item(i).Name = "Control_" & Cell
TableLayoutPanel1.Controls.Item(i).Enabled = True
TableLayoutPanel1.Controls.Item(i).Visible = True
'here we add a handler for the picture boxs click event
AddHandler oPicture.Click, AddressOf oPictureClickEvent
Next
TableLayoutPanel1.Visible = True
For i As Integer = 0 To TableLayoutPanel1.Controls.Count - 1
TableLayoutPanel1.Controls(i).Enabled = True
TableLayoutPanel1.Controls(i).Visible = True
Next
End Sub
'' das passiert nicht automatisch'. Es tut. Sieht aus wie ein einzelner Fehler, der dadurch verursacht wird, dass der Zeilenzähler auf -1 statt auf 0 initialisiert wurde. Stellen Sie sicher, dass das TLP überhaupt keine Zeilen mehr enthält, nachdem Sie es auf dem Formular abgelegt haben. –
Wenn der Code zum TableLayoutPanel gelangt, ist der Zeilenwert 0. –
Zeile 1 füllt sich wie erwartet und ist sichtbar. Es sind die zusätzlichen Zeilen, die nie sichtbar sind. Aber die Reihen sind da und wurden gefüllt. –