2016-06-22 5 views
-1

이미지를 TableLayoutPanel에 동적으로 추가하려고합니다. 추가 기능이 작동하는 것처럼 보입니다. 컨트롤의 내용을 보면 새 항목이 추가 된 것처럼 보입니다. RowCount = 10, IEnnumerable에는 내가 추가 한 컨트롤이 포함 된 18 개의 항목이 있습니다.TableLayoutPanel add 컨트롤이 보이지 않습니다.

제 문제는 양식이 표시 될 때 첫 번째 행만 표시된다는 것입니다. 동적으로 Visible 속성을 True로 설정할 수 없으므로 명령이 무시됩니다. 나는 무엇을 놓치고 있습니까?

코드 :

TableLayoutPanel 높이를 설정되었다 무엇
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

'이것은 자동으로 발생하지 않습니다.' 그렇습니다. 행 카운터를 0 대신 -1로 초기화하여 발생하는 off-by-one 버그와 같이 보입니다. TLP에 양식을 놓은 후 TLP에 행이 없도록하여 코드가 이해되기 시작하도록하십시오. –

+0

코드가 TableLayoutPanel에 도착할 때 행 값은 0입니다. –

+0

행 1이 예상대로 채워져 표시됩니다. 그것은 결코 보이지 않는 추가 행입니다. 그러나 행이 거기에 채워져 있습니다. –

답변

0

. 컨트롤을 추가 한 후에 테이블 높이를 행 높이 * (행 수 - 1)의 배수로 조정해야합니다. 기본 요구 사항이지만 마지막 행이 늘어나지 않도록 조정해야합니다.

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