将列表中的每个值设置为vb中的某些值

时间:2014-08-15 18:14:06

标签: vb.net list picturebox

假设我想设置列表中每个项目的属性,如下所示:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim test As New List(Of PictureBox)


    For q = 1 To 25
        Dim picbox As New PictureBox
        test.Add(picbox)


    Next

计时器勾选

    test.Item(everything in list).Top -= 3
End Sub

我可以一次完成所有操作,而不是分别迭代和设置每个值吗?

1 个答案:

答案 0 :(得分:0)

您应该在将颜色添加到列表之前更新颜色:

For q = 1 To 25
    picBox.BackColor = Color.AliceBlue
    test.Add(picbox)
Next

顺便说一句,您是否意识到您要添加相同的项目25次?如果你想要不同的实例,你需要在循环中创建一个新实例:

For q = 1 To 25
    picBox = New PictureBox
    picBox.BackColor = Color.AliceBlue
    test.Add(picbox)
Next

只是为了好玩,你可以像这样重写你的整个片段,以获得25个PictureBoxes的列表:

Dim test = Enumerable.Range(1,25) _
           .Select(Function(i) New PictureBox With {.BackColor = Color.AliceBlue}) _
           .ToList