基于列表框项的数量向表单添加控件

时间:2012-09-14 02:57:48

标签: vb.net visual-studio-2010 listbox controls

我想出了如何根据添加到列表框中的项目数量向表单添加控件。

    ListBox1.Items.Add(TextBox1.Text)
    TextBox1.Text = ""

    For i As Integer = 0 To ListBox1.Items.Count - 1

        'adds picturebox for as many listbox items added
        Dim MyPictureBox As New PictureBox()
        MyPictureBox.Location = New Point(25, 25)
        MyPictureBox.Size = New Size(15, 15)
        MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
        Me.Controls.Add(MyPictureBox)
        MyPictureBox.Image = My.Resources.PDF_Info

    Next i

我似乎无法弄清楚如何添加第二个控件,但将其添加到表单上的其他位置。

所以第一个条目会将图片框放在25,25,当我输入第二个项目时,它会在45,45等处创建另一个图片框。

可以这样做吗?

2 个答案:

答案 0 :(得分:1)

  ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
Dim x As Integer = 25
For i As Integer = 0 To ListBox1.Items.Count - 1

    'adds picturebox for as many listbox items added
    Dim MyPictureBox As New PictureBox()
    MyPictureBox.Location = New Point(x, x)
    MyPictureBox.Size = New Size(15, 15)
    MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
    Me.Controls.Add(MyPictureBox)
    MyPictureBox.Image = My.Resources.PDF_Info
    x += 25
Next i

答案 1 :(得分:1)

它将是这样的

ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
Dim p as Integer = 25

For i As Integer = 0 To ListBox1.Items.Count - 1
    'adds picturebox for as many listbox items added
    Dim MyPictureBox As New PictureBox()
    MyPictureBox.Location = New Point(p , p)
    MyPictureBox.Size = New Size(15, 15)
    MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
    Me.Controls.Add(MyPictureBox)
    MyPictureBox.Image = My.Resources.PDF_Info
   'add 20  to the current position of the control
   p = p + 20

Next i