Vb图片框(n)

时间:2018-03-15 14:06:07

标签: vb.net

 Dim x As String
    Dim zombies As List(Of Zombie)
    Dim n As Integer
    Dim boxes As List(Of PictureBox)

    x = diceRoll(4)
    RichTextBox2.Text &= "You rolled a " + x & vbCrLf
    RichTextBox1.Text &= "There are " + x + " Zombies attacking" & vbCrLf
    zombies = makeZombies(Val(x), 4)' x zom w/ 4 max health

    n = 0

    boxes.Add(PictureBox1)
    boxes.Add(PictureBox2)
    boxes.Add(PictureBox3)
    boxes.Add(PictureBox4)




    For i = 1 To Val(x)
        n = n + 1
        boxes(n).Image = My.Resources.zombie



    Next

    fight(zombies)
End Sub

我有4个图片框,我试图在骰子上滚动一个数字(diceroll(4)),这取决于你滚动的图片框的图像会改变。如果一个3被滚动,4个图片框中的3个将改变为图片,否则他们是清晰的图片框。图像在我的资源中称为zombie.png。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我可以看到一些问题。一个是数组/列表索引从0开始而不是1,但n变量在第一次用作索引之前递增为1。此外,您没有初始化boxes集合...当您尝试使用它时,它是一个空引用/无。

此代码修复了这些问题并进行了其他一些清理:

Dim x As Integer = diceRoll(4) 'If this function returns a string, fix it to use Integer instead
Dim zombies As List(Of Zombie) = makeZombies(x, 4)' x zom w/ 4 max health

Dim boxes As New List(Of PictureBox) From {PictureBox1, PictureBox2, PictureBox3, PictureBox4}
boxes = boxes.Take(x).ToList()

RichTextBox2.Text &= "You rolled a " + x.ToString() & vbCrLf
RichTextBox1.Text &= "There are " + x.ToString() + " Zombies attacking" & vbCrLf

For Each box As PictureBox in boxes
    box.Image = My.Resources.zombie
Next

fight(zombies)