For Loop on imageList (picturebox) -

Posted on 2012-09-19
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 85

    Accepted Solution

    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

    Thanks Mike ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    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…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now