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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25515773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:06:30  来源:igfitidea点击:

How to Pass Image in ImageList to PictureBox in Reverse Index Order

vb.netpictureboximagelist

提问by DesignerMind

I have a VB.Net application which has spelling words that a child can type in and it verifies the spelling. On the form, I have a Nextand Backbutton, which advances a PictureBoxwith items contained in an ImageList. I have 3 items in the ImageListcollection with an index[2].

我有一个 VB.Net 应用程序,它有一个孩子可以输入的拼写单词,它可以验证拼写。在表单上,​​我有一个NextBack按钮,它PictureBox使用ImageList. 我在ImageList集合中有 3 个带有index[2].

Here's the code that I'm using to advance the Next button to display the next image in the collection:

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

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

While this works, I cannot figure out how to get this to work in the reverse order. Here's the OnLoadevent handler that starts with the first image in the collection:

虽然这有效,但我无法弄清楚如何让它以相反的顺序工作。这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

How can I get the Backbutton to go backwards in the index from it's current point in the index which is shown in the PicutureBoxcontrol? This is where I'm stumped. Tried to re-write the code several times, if I click Nextonce, I go to the next image index[1]and if click the Backbutton, it takes me to index[0].

如何让“后退”按钮从PicutureBox控件中显示的索引中的当前点在索引中向后移动?这是我难住的地方。多次尝试重新编写代码,如果我单击Next一次,我会转到下一张图像index[1],如果单击Back按钮,它会将我带到index[0].

But if click next again, PictureBox jumps to index[2]the last image, rather than going back to index[1]. If I click Backagain, the code is blowing up.

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

采纳答案by DesignerMind

First of all, I would like to give thanks to both Ken White, and valter, for assisting me and opening up my thinking in this approach. I used Ken's advice on my variable naming and valter's approach for my first solution of the btnNextproblem.

首先,我要感谢 Ken White 和 valter,他们在这种方法中帮助我并打开了我的思路。我在我的第一个btnNext问题解决方案中使用了 Ken 关于我的变量命名的建议和 valter 的方法。

Thus, as I had a working btnNextto flip through my images in the imagelist1collection and push them to the picturebox1, I took Ken's advice arranged my code and renamed variables to get btnPreviousto work as such:

因此,我有一个工作btnNext,通过我的图像在翻转imagelist1的收集和他们推到了picturebox1,我带着肯的建议安排我的代码,并更名为变量得到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

This is what I was trying to accomplish with btnPrevious, without it skipping around or out of order. Although the code can skip around via Next, the user cannot go backward beyond the first image in the Imagebox1index[0]. This is what I wanted. Again, a BIG THANKS to those who helped me to think it out. That's what being on Stack is all about... learn from the answers you get, and run free developing in confidence... like a happy baby w/out a diaper! Thanks fellas!

这就是我试图用 完成的btnPrevious,而不是跳过或乱序。虽然代码可以通过 Next 跳过,但用户不能返回到Imagebox1index[0]. 这就是我想要的。再次,非常感谢那些帮助我思考的人。这就是在 Stack 上的全部意义......从你得到的答案中学习,并自信地自由发展......就像一个没有尿布的快乐婴儿!谢谢各位!

回答by Ken White

Move your countvariable (which is badly named, BTW - I've used CurrIdxinstead) to a higher visibility. In the previous button's click handler, decrement the index; if it drops below 0, reset it to the ImageList.Images.Count - 1again. I'd also move the code that actually sets the image index to its own procedure, so that you're not repeating yourself and it's more clear. Something like this should work for you:

将您的count变量(名称很糟糕,顺便说一句 - 我用过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