0

我正在尝试将控件添加到具有垂直间距的面板中?

这是我现在的代码:

 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。我这样做了,如果内容高于面板高度,则有滚动条。代码在同一个位置添加所有控件,所以我只能看到最后一张照片。

我该怎么做才会有一张照片,然后是一个标签,然后是另一张照片等等

谢谢!

4

1 回答 1

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)
于 2013-02-02T20:47:08.833 回答