vb.net 如何在 TableLayoutPanel 中设置行高
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14711948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set row's height in TableLayoutPanel
提问by kfirba
I'm dynamically adding rows into my TableLayoutPanelbut I just can't configure there height.
我正在动态地将行添加到我的行中,TableLayoutPanel但我无法配置那里的高度。
The code might look long, but it's a very simple one.
代码可能看起来很长,但它是一个非常简单的代码。
An explanation about the code:
关于代码的解释:
The code creates a TableLayoutPaneland set it's properties. After that, the code creates Pictureboxesand Labelsaccording to how many movies there are in the database. After creating a Pictureboxand a Labelthe code puts both of them in a Paneland then the code inserts the Panelinto the TableLayoutPanel. The problem is the row's height.
代码创建一个TableLayoutPanel并设置它的属性。在此之后,代码创建Pictureboxes,并Labels根据多少部电影有在数据库中。创建 aPicturebox和 a 后Label,代码将它们都放在 a 中Panel,然后代码将 插入Panel到TableLayoutPanel. 问题是行的高度。
The output:
输出:


Here is the code I'm using:
这是我正在使用的代码:
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
Thanks in advance!
提前致谢!
采纳答案by kfirba
I got an answer. In order to set the height of the row, all you have to do is is to add this:
我得到了答案。为了设置行的高度,您所要做的就是添加以下内容:
tablePanel.RowStyles.Add(New RowStyle(SizeType.Absolute, 150))
you should add this line afteradding the Panelinto the TableLayoutPanel
您应该在添加Panel到TableLayoutPanel
A snippet:
一个片段:
'.... 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
Hope that helps someone
希望能帮助某人
回答by Abdusalam Ben Haj
Try This :
尝试这个 :
For Each RS As RowStyle In tablePanel.RowStyles
RS.SizeType = SizeType.Absolute
RS.Height = 180
Next

