TableLayoutPanel添加控件不可见

时间:2016-06-22 16:27:55

标签: vb.net tablelayoutpanel

我正在尝试动态地将图像添加到TableLayoutPanel。添加似乎有效,当我查看控件的内容时,新项似乎已添加。 RowCount = 10,IEnnumerable有18个项目,包含我添加的控件。

我的问题是,当显示表单时,只有第一行可见。我无法动态地将Visible属性设置为True,忽略该指令。我错过了什么?

代码:

   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

1 个答案:

答案 0 :(得分:0)

缺少的是设置TableLayoutPanel高度。添加控件后,您需要将表的高度调整为行的高度的倍数*(行数 - 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