在vb.net中以编程方式将多个图片框添加到表单中?

时间:2015-07-06 10:53:39

标签: vb.net picturebox

我必须按照我的要求将图片框添加到面板中。

Adding multiple pictureboxes to a form programmatically in vb.net”在这个问题中,PictureBox是随机绘制的,但我希望它以同步的方式     在这里输入代码

Dim i As String = ListBox1.Items.Count
For j As Integer = 0 To i
Dim PicBox As New PictureBox
PicBox.Width = 40
PicBox.Top = 25
PicBox.Left = j + 15
PicBox.SizeMode = PictureBoxSizeMode.StretchImage
PicBox.BorderStyle = BorderStyle.FixedSingle
Me.Panel1.Controls.Add(PicBox)
Next

我想使用自动检查i的值的计数器?

有任何想法或建议吗?

谢谢

1 个答案:

答案 0 :(得分:3)

这样的事情怎么样:

Private Sub PicBoxTestButton_Click(sender As System.Object, e As System.EventArgs) Handles PicBoxTestButton.Click
    Try
        Dim numberOfPics As Integer = ListBox1.Items.Count
        Dim lastLeft As Integer = 15
        Const spacer As Integer = 5
        For parser As Integer = 0 To numberOfPics
            Dim PicBox As New PictureBox
            PicBox.Width = 40
            PicBox.Top = 25
            PicBox.Left = lastLeft
            lastLeft = PicBox.Width + PicBox.Left + spacer
            PicBox.SizeMode = PictureBoxSizeMode.StretchImage
            PicBox.BorderStyle = BorderStyle.FixedSingle
            Me.Panel2.Controls.Add(PicBox)
        Next
    Catch ex As Exception
        MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
    End Try
End Sub