如何将ImageList中的图像以反向索引顺序传递给PictureBox

时间:2014-08-26 21:52:32

标签: vb.net picturebox imagelist

我有一个VB.Net应用程序,它有一个孩子可以输入的拼写单词并验证拼写。在表单上,​​我有一个下一步后退按钮,该按钮会使PictureBox中包含的项目前进ImageList。我在ImageList集合中有3个项目index[2]

这是我用来推进“下一步”按钮以显示集合中下一张图片的代码:

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    'Get images and place them in the Imagebox on each click.

    Static count As Integer = 1

    If count > ImageList1.Images.Count - 1 Then
        count = 0
    End If
    PictureBox1.Image = ImageList1.Images(count)
    count += 1        

End Sub

虽然这有效,但我无法弄清楚如何以相反的顺序使其工作。这是OnLoad事件处理程序,它以集合中的第一个图像开头:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Set picturebox with initial picture

    PictureBox1.Image = ImageList1.Images(0)
    txtSpell.Focus()

End Sub

如何让后退按钮从索引中的当前点(PicutureBox控件中显示)向后转到索引中?这是我难倒的地方。试图多次重新编写代码,如果我点击下一步一次,我会转到下一张图片index[1],如果点击后退按钮,则需要我到index[0]

但如果再次点击下一步,PictureBox会跳转到index[2]最后一张图片,而不是返回index[1]。如果我再次点击返回,代码就会爆炸。

2 个答案:

答案 0 :(得分:1)

将您的count变量(名称错误,BTW - 我已使用CurrIdx)移动到更高的可见度。在上一个按钮的单击处理程序中,减少索引;如果它低于0,则再次将其重置为ImageList.Images.Count - 1。我还将实际设置图像索引的代码移动到自己的程序中,这样您就不会重复自己,而且更清晰。这样的事情对你有用:

Private CurrIdx As Integer = 0

Private Sub UpdateImage()
    PictureBox1.Image = ImageList1.Images(CurrIdx)
End Sub

Private Sub PrevButton_Click(sender As Object, e As EventArgs) Handles PrevButton.Click
    CurrIdx -= 1
    If CurrIdx < 0 Then
        CurrIdx = ImageList1.Images.Count - 1
    End If
    UpdateImage()

End Sub

Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
    CurrIdx += 1
    If CurrIdx > ImageList1.Images.Count - 1 Then
        CurrIdx = 0
    End If
    UpdateImage()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    UpdateImage()
End Sub

答案 1 :(得分:0)

首先,我要感谢Ken White和Valter,感谢他们帮助我并开始思考这种方法。我在我的变量命名和valter方法上使用了Ken的建议来解决btnNext问题的第一个问题。

因此,当我有一个工作btnNext翻阅imagelist1集合中的图片并将其推送到picturebox1时,我接受了Ken的建议安排了我的代码并重命名变量以使btnPrevious能够正常工作:

Private CounterVar As Integer = 0

Private Sub ImageUpdate()
    'Sets the picture box to the first image of the series.
    PictureBox1.Image = ImageList1.Images(CounterVar)

End Sub

Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
    'Allow the Back button to respond the appropriate PictureBox item.
    If CounterVar = 1 Then   'This is the second imagelist item, index[1].
        CounterVar = 0       'This is the first imagelist item, index[0].
        ImageUpdate()

    ElseIf CounterVar = 2 Then   'This is the last imagelist item, index[2]. 
        CounterVar = 1           'This is the second imagelist item, index[1].
        ImageUpdate()

    Else
        CounterVar = CounterVar  'Otherwise, the count remains where it is till Back is clicked.
    End If
    ImageUpdate()

End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    'Setup counter variable to begin the button processing.
    CounterVar += 1

    'Get Imagebox items and place them in the PictureBox, in order, on each click.
    If CounterVar > ImageList1.Images.Count - 1 Then
        CounterVar = 0
    End If
    ImageUpdate()

    'On a correct answer, clear the textbox, spell label, and hide the result label,
    'then set the focus of the textbox.

    txtSpell.Text = String.Empty
    lblAnsResult.Text = String.Empty
    lblAnsResult.Visible = False
    txtSpell.Focus()

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Set imagebox to first index image.
    ImageUpdate()

    'Set focus to the textbox.
    txtSpell.Focus()

End Sub

这是我试图用btnPrevious完成的,没有它跳过或乱序。虽然代码可以通过“下一步”跳过,但用户无法返回Imagebox1 index[0]中的第一张图片。这就是我想要的。再一次,对那些帮助我思考它的人表示非常感谢。那就是Stack上的内容就是......从你得到的答案中学习,并自由地发展自信......就像一个快乐的宝宝带着尿布!谢谢fellas!