我正在向TableLayoutPanel
动态添加行,但我无法配置高度。
代码可能看起来很长,但这是一个非常简单的代码。
有关代码的说明:
代码创建TableLayoutPanel
并设置其属性。之后,代码根据数据库中有多少部电影创建Pictureboxes
和Labels
。创建Picturebox
和Label
代码后,代码会将它们放在Panel
中,然后代码会将Panel
插入TableLayoutPanel
。问题是行的高度。
输出:
以下是我正在使用的代码:
Dim movieN As Integer = MoviesDataSet.movies.Rows.Count
Dim tablePanel As New TableLayoutPanel
With tablePanel
.Size = New Point(Me.ClientRectangle.Width - 10, Me.ClientRectangle.Bottom - 55)
.ColumnCount = 4
.GrowStyle = TableLayoutPanelGrowStyle.AddRows
.AutoScroll = True
.Margin = New System.Windows.Forms.Padding(0)
.Location = New System.Drawing.Point(5, 50)
.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 25.0!))
.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 25.0!))
.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 25.0!))
.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 25.0!))
.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
End With
For Each MovieRow As DataRow In MoviesDataSet.Tables("movies").Rows
'define two new controls to be added
Dim myLabel As New Label
Dim myPicture As New PictureBox
Dim container As New Panel
'set the properties of the new controls
myLabel.Text = MovieRow("movieName")
myLabel.AutoSize = True
myLabel.Location = New System.Drawing.Point(30, 110)
With myPicture
.Image = Image.FromFile(MovieRow("moviePhoto"))
.Tag = MovieRow("ID")
.Size = New System.Drawing.Size(100, 100)
.SizeMode = PictureBoxSizeMode.StretchImage
.Location = New System.Drawing.Point(2, 2)
.Cursor = Cursors.Hand
End With
'here we add the controls to a layout panel to
'manage the positioning of the controls
With container
.Dock = DockStyle.Fill
.Margin = New System.Windows.Forms.Padding(0)
.Controls.Add(myPicture)
.Controls.Add(myLabel)
End With
With tablePanel.Controls
.Add(container)
End With
'here we add a handler for the picture boxs click event
AddHandler myPicture.Click, AddressOf MyPictureClickEvent
Next
Me.Controls.Add(tablePanel)
End Sub
提前致谢!
答案 0 :(得分:8)
试试这个:
For Each RS As RowStyle In tablePanel.RowStyles
RS.SizeType = SizeType.Absolute
RS.Height = 180
Next
答案 1 :(得分:1)
我得到了答案。要设置行的高度,您只需添加以下内容:
tablePanel.RowStyles.Add(New RowStyle(SizeType.Absolute, 150))
您应该在之后添加此行将Panel
添加到TableLayoutPanel
摘录:
'.... THE CODE ABOVE CAN BE SEEN IN THE QUESTION POST
With container
.Dock = DockStyle.Fill
.Margin = New System.Windows.Forms.Padding(0)
.Controls.Add(myPicture)
.Controls.Add(myLabel)
End With
With tablePanel.Controls
.Add(container)
End With
tablePanel.RowStyles.Add(New RowStyle(SizeType.Absolute, 150))
'here we add a handler for the picture boxs click event
AddHandler myPicture.Click, AddressOf MyPictureClickEvent
Next
Me.Controls.Add(tablePanel)
End Sub
希望能帮到某人