我正在尝试使用垂直间距向我的面板添加控件?
这是我现在的代码:
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim pic As PictureBox = New PictureBox()
Dim lb As Label = New Label()
Dim cells As Integer = MoviesDataSet.movies.Count
ReDim mypb(cells)
ReDim mylb(cells)
Dim k As Integer = 0
For Each movie As DataRow In MoviesDataSet.movies
pic.ImageLocation = Application.StartupPath & "\" & movie("moviePhoto")
pic.Size = New System.Drawing.Size(x, y)
pic.SizeMode = PictureBoxSizeMode.StretchImage
pic.Margin = New System.Windows.Forms.Padding(0, 0, 0, 5)
mypb(k) = pic
lb.Text = movie("movieName")
lb.AutoSize = True
lb.Margin = New System.Windows.Forms.Padding(0, 0, 0, 10)
mylb(k) = lb
k += 1
Next
For i As Integer = 0 To MyPB.Count - 1 Step 1
Panel1.Controls.Add(MyPB(i))
Panel1.Controls.Add(MyLb(i))
Next
End Sub
我的面板宽度约为180,高度约为170.如果内容高于面板高度,我会这样做,有滚动条。代码是在同一个地方添加所有控件,所以我只能看到最后一张照片。
我该怎么做才会有照片,然后是标签,然后是另一张照片等等
谢谢!
答案 0 :(得分:1)
如果您希望放置在控件中的图像垂直对齐而不是全部位于同一位置,则需要在将控件放入控件之前为控件计算新位置。
Dim index As Integer = 0
For i As Integer = 0 To 10
Dim picture As New PictureBox() With
{
.Size = New Size(100, 30),
.Location = New Point(10, .Height + (i * 40)),
.BackColor = Color.Red
}
Panel1.Controls.Add(picture)
Next
我显然无法测试你的代码,但这种逻辑应该足够了:
pic.Location = new Point(10, pic.Height + (k * 40)