2016-06-22 17 views
-1

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 
+0

'' 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. –

+0

Wenn der Code zum TableLayoutPanel gelangt, ist der Zeilenwert 0. –

+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. –

Antwort

0

Was die Table Höhe fehlte einstellte. Nachdem Sie die Steuerelemente hinzugefügt haben, müssen Sie die Höhe der Tabelle auf ein Vielfaches der Höhe der Zeilen anpassen * (Anzahl der Zeilen - 1), das ist die Grundvoraussetzung, muss aber angepasst werden, um die letzte Zeile zu strecken. .

Private Sub LoadTable() 

    Dim oPictures As Control() = {} 
    'Dim oTableLayoutPanel As New TableLayoutPanel 
    ' 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 

    TableLayoutPanel1.RowCount = Fix(m_FileList.Count/2) 

    ' 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 
      TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, Height)) 
      ' this doesn't happen automatically 
      TableLayoutPanel1.RowCount = Rows + 1 
     Else 
      ' this line should never be needed but just in case 
      If Rows < 0 Then Rows = 0 
      Cols = 1 
     End If 
     ' this is used for the name of some controls 
     Cell = "R" & Rows & "C" & Cols 

     '-------------------------------------------- 
     ' set up the PictureBox 
     '-------------------------------------------- 
     Dim oPictureBox As New PictureBox 
     ' 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) 
     ' create the PictureBox 
     With oPictureBox 
      .Height = Height 
      .Width = Width 
      ' load the image from the current file path 
      Dim oImage As Bitmap = New Bitmap(Bitmap.FromFile(CType(m_FileList.Item(i).Value, String))) 

      ' set the PictureBox properties 
      .BackgroundImageLayout = ImageLayout.Stretch 
      .BorderStyle = BorderStyle.Fixed3D 
      ' scale the image to the PictureBox size 
      ScaleImage(oPictureBox, oImage) 
      ' set the Image of the PictureBox 
      .Image = oImage 
      .Name = "PictureBox" & i + 1 
      .Dock = DockStyle.Fill 
      .Size = New System.Drawing.Size(Height, Width) 
      .SizeMode = PictureBoxSizeMode.StretchImage 
      .Location = New System.Drawing.Point(0, 0) 
      .Cursor = Cursors.Hand 
      .Visible = True 
      .Enabled = True 

      ' set the Tag to the filepath of the image 
      .Tag = f 

      ' set the ToolTip for the PictureBox 
      ' This becomes the "Title" from the File MetaData 
      SetToolTip(oPictureBox, f) 

      'here we add a handler for the PictureBox click event 
      AddHandler oPictureBox.Click, AddressOf PictureClickEvent 
     End With 
     oPictures.Add(oPictureBox) 

    Next 

    ' now add the picturebox to the next cell addres 
    With TableLayoutPanel1 
     ' add the 
     .Controls.AddRange(oPictures) 
     '.Controls.Add(oPictureBox, Cols, Rows) 
     '.SetRow(oPictureBox, Rows) 
     '.SetColumn(oPictureBox, Cols) 
     '.Controls.Item(i).Name = "Control_" & Cell 
     '.Controls.Item(i).Enabled = True 
     '.Controls.Item(i).Visible = True 
    End With 
    TableLayoutPanel1.Visible = True 

    For i As Integer = 0 To TableLayoutPanel1.Controls.Count - 1 
     TableLayoutPanel1.Controls(i).Enabled = True 
     TableLayoutPanel1.Controls(i).Visible = True 
    Next 
    ' adjust the height of the table to a multiple of the number of new rows * the Height of each row 
    TableLayoutPanel1.Height = (TableLayoutPanel1.RowCount - 1) * Height 

    ' set the TableLayoutPanel properties 
    TableLayoutPanel1.Dock = DockStyle.Top 
    TableLayoutPanel1.AutoSize = False 
    TableLayoutPanel1.Visible = True 
    TableLayoutPanel1.Enabled = True 
    TableLayoutPanel1.Focus() 
    TableLayoutPanel1.Parent = Me.Panel1 
End Sub 
Verwandte Themen