gpt4 book ai didi

vb.net - 如何设置 TableLayoutPanel 单元格的大小

转载 作者:行者123 更新时间:2023-12-01 13:58:45 26 4
gpt4 key购买 nike

我正在尝试在每个单元格中制作一个带有 PictureBox + LabelTableLayoutPanel。我没问题,但我无法将单元格大小设置为相同!我试图有 4 列无限行数,我希望单元格在 Label 宽度内,除非 Label 宽度小于 图片宽度。

目前,我的代码几乎可以正常工作,只是没有设置单元格大小,因为我不知道该怎么做。

这是我的代码:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim movieN As Integer = MoviesDataSet.movies.Rows.Count
Dim tablePanel As New TableLayoutPanel

With tablePanel
.Size = New Point(650, 450)
.ColumnCount = 4
.GrowStyle = TableLayoutPanelGrowStyle.AddRows
.AutoScroll = True
.Margin = New System.Windows.Forms.Padding(0)
.Location = New System.Drawing.Point(5, 50)
.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
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.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)
End With

'here we add the controls to a flow layout panel to
'manage the positioning of the controls but you could
'explicitly set the location of the controls if you
'just wanted to add them to the forms controls collection
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
AddHandler myPicture.MouseHover, AddressOf MyPictureHoverEvent
Next
Me.Controls.Add(tablePanel)
End Sub

最佳答案

如果您希望单元格宽度与 PictureBoxLabel 中较宽的宽度相同,并且通过将它们包裹起来将它们放在同一单元格中在 Panel 中然后设置 PanelAutoSizeMode AutoSizeMode.GrowAndShrink 及其 AutoSize 属性属性为 True。如果您随后对 TableLayoutPanel 执行相同的操作,它将使每个单元格根据 Panel 的大小增大或缩小,而 Panel 将根据其中控件的大小执行相同的操作。

关于vb.net - 如何设置 TableLayoutPanel 单元格的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com