如何在不手动将控件添加到表单的情况下用控件填充 tableLayoutPanel?
How do you fill a tableLayoutPanel with controls without manually adding them to the form?
我正在编写一个程序来通过多个 PictureBoxes 显示图像,并认为 tableLayoutPanel
是实现此目的的好方法。我目前有一个 50x50 的网格。目的是用 PictureBoxes
.
填充每一列和每一行
我以后还需要能够调整这个网格的大小,所以我无法拖放图片框。
我找过任何试图做同样事情的人,并遇到过 this 但它是用 C# 编写的,我还不够好,无法将其翻译过来。
我曾尝试这样做,但我不知道如何在运行时开始添加图片框,而且我怀疑我在咆哮错误的方法树。
Dim tableSquareCollection = tableLayoutPanel1.Controls.OfType(Of TableLayoutPanel)() 'get collection of how many squares are in the table - this doesn't work, I can't find what datatype an empty square is
For i = 0 To tableSquareCollection.Count - 1
'Add picture boxes to each square - but I have no ideas at how to start this
Next
我知道这是对我的问题的最小尝试,但我看不到任何进一步实现我的目标的方法。
谢谢。
您发布的代码没有任何意义。如果您的目标是向 TableLayoutPanel
添加控件,为什么要遍历 Controls
集合,它是现有控件的列表?此外,您为什么要尝试在 TableLayoutPanel
中获取 TableLayoutPanels
?
如果您的目标是将 PictureBox
添加到 TableLayoutPanel
,那么这就是您要做的。您需要先创建一个PictureBox
,然后添加它:
Dim pb As New PictureBox
tableLayoutPanel1.Controls.Add(pb)
控件将自动添加到下一个可用单元格。如果你有 50 行乘 50 列,那么你需要一个 50x50 次迭代的循环:
For i = 1 To tableLayoutPanel1.RowCount * tableLayoutPanel1.ColumnCount
Dim pb As New PictureBox
tableLayoutPanel1.Controls.Add(pb)
Next
也就是说,表格上的 2500 PictureBoxes
几乎肯定太多了。如果您想调整表格大小并让 TableLayoutPanel
随表格展开和收缩,则效果不会很流畅。
我建议您改用 FlowLayoutPanel
。这将处理调整大小:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FlowLayoutPanel1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Right
FlowLayoutPanel1.BorderStyle = BorderStyle.Fixed3D
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Red)})
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Black)})
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Orange)})
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Green)})
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Blue)})
End Sub
Function GetBitmap(br As Brush) As Bitmap
Dim b = New Bitmap(100, 100)
Using g As Graphics = Graphics.FromImage(b)
g.FillRectangle(br, New Rectangle(0, 0, 100, 100))
End Using
Return b
End Function
我正在编写一个程序来通过多个 PictureBoxes 显示图像,并认为 tableLayoutPanel
是实现此目的的好方法。我目前有一个 50x50 的网格。目的是用 PictureBoxes
.
我以后还需要能够调整这个网格的大小,所以我无法拖放图片框。
我找过任何试图做同样事情的人,并遇到过 this 但它是用 C# 编写的,我还不够好,无法将其翻译过来。
我曾尝试这样做,但我不知道如何在运行时开始添加图片框,而且我怀疑我在咆哮错误的方法树。
Dim tableSquareCollection = tableLayoutPanel1.Controls.OfType(Of TableLayoutPanel)() 'get collection of how many squares are in the table - this doesn't work, I can't find what datatype an empty square is
For i = 0 To tableSquareCollection.Count - 1
'Add picture boxes to each square - but I have no ideas at how to start this
Next
我知道这是对我的问题的最小尝试,但我看不到任何进一步实现我的目标的方法。
谢谢。
您发布的代码没有任何意义。如果您的目标是向 TableLayoutPanel
添加控件,为什么要遍历 Controls
集合,它是现有控件的列表?此外,您为什么要尝试在 TableLayoutPanel
中获取 TableLayoutPanels
?
如果您的目标是将 PictureBox
添加到 TableLayoutPanel
,那么这就是您要做的。您需要先创建一个PictureBox
,然后添加它:
Dim pb As New PictureBox
tableLayoutPanel1.Controls.Add(pb)
控件将自动添加到下一个可用单元格。如果你有 50 行乘 50 列,那么你需要一个 50x50 次迭代的循环:
For i = 1 To tableLayoutPanel1.RowCount * tableLayoutPanel1.ColumnCount
Dim pb As New PictureBox
tableLayoutPanel1.Controls.Add(pb)
Next
也就是说,表格上的 2500 PictureBoxes
几乎肯定太多了。如果您想调整表格大小并让 TableLayoutPanel
随表格展开和收缩,则效果不会很流畅。
我建议您改用 FlowLayoutPanel
。这将处理调整大小:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FlowLayoutPanel1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Right
FlowLayoutPanel1.BorderStyle = BorderStyle.Fixed3D
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Red)})
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Black)})
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Orange)})
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Green)})
FlowLayoutPanel1.Controls.Add(New PictureBox With {.Image = GetBitmap(Brushes.Blue)})
End Sub
Function GetBitmap(br As Brush) As Bitmap
Dim b = New Bitmap(100, 100)
Using g As Graphics = Graphics.FromImage(b)
g.FillRectangle(br, New Rectangle(0, 0, 100, 100))
End Using
Return b
End Function