For Loop on imageList (picturebox) - vb.net

Posted on 2012-09-19
Medium Priority
Last Modified: 2012-09-19
Hi Experts,

I have a winform with a Button, picturebox and imagelist. The imagelist only has 3 images embedded.

I simply want to click button1 and it shows image0, then if clicked again, image1, then if clicked again.. image 2... then loop if clicked.
 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For i = 0 To 2
            PictureBox1.Image = ImageList1.Images(i)
            i = i + 1
    End Sub

Open in new window

My code only shows image0...

Question by:RobertoFreemano
LVL 86

Accepted Solution

Mike Tomlinson earned 800 total points
ID: 38415818
Try something more like:
    Private index As Integer = -1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        index = index + 1
        If index > ImageList1.Images.Count - 1 Then
            index = 0
        End If
        PictureBox1.Image = ImageList1.Images(index)
    End Sub

Open in new window


Author Closing Comment

ID: 38416570
Thanks Mike ;)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question