?
Solved

Diplaying an Imagelist with animation in a picture box

Posted on 2009-04-26
7
Medium Priority
?
402 Views
Last Modified: 2012-06-21
I am trying to setup a picturebox that will display a total of 5 images in a sequence to look like animation. I am getting one error in regard to the following;
"A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll"
Don't know why. I've loaded the images and tried setting the # of images @ 0, 3, & 5, but still get the error no matter what the #.  I also need two buttons, one to be able to speed up the images in the picturebox and one to speed down the images in the picturebox.

Here is the code I am using. Please be very specific.  I am new to programming and really need this completed by today as it is a test my work is putting me through.
Public Class Form1
    ' A form level variable to hold which frames is to show which image
    ' It has to be form level so that our timer1_tick event 
    Dim imageNumber As Integer = 0
    Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
 
    End Sub
    ' Our form load even just sets the pictureboxe's image
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.Image = ImageList1.Images(0)
 
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        PictureBox1.Image = ImageList1.Images(Imagenumber)
        Imagenumber = Imagenumber + 1
        If Imagenumber > ImageList1.Images.Count - 1 Then
            Imagenumber = 0
        End If
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Timer1.Enabled Then
            Timer1.Stop()
        Else
            Timer1.Start()
        End If
    End Sub
End Class

Open in new window

0
Comment
Question by:pcmedic4u
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24237172
I played with it a bit and can't get it to fail with this code:
Public Class Form1
 
    Private imageNumber As Integer = 0
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If ImageList1.Images.Count > 0 Then
            imageNumber = imageNumber + 1
            If imageNumber = ImageList1.Images.Count Then
                imageNumber = 0
            End If
            PictureBox1.Image = ImageList1.Images(imageNumber)
        End If
    End Sub
 
End Class

Open in new window

0
 

Author Comment

by:pcmedic4u
ID: 24237419
Here is what I get when adding your code. I only think this is off a little bit-not your code but the Form as a whole. Thanks for the help.

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        PictureBox1.Image = ImageList1.Images(0)

This line is causing the error below-
Argument out of range exception was unhandled.

Here is all of the code including the changes I made with your code.
 
Public Class Form1
    ' A form level variable to hold which frames is to show which image
    ' It has to be form level so that our timer1_tick event 
    Private imageNumber As Integer = 0
 
    Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
 
    End Sub
    ' Our form load even just sets the pictureboxe's image
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        PictureBox1.Image = ImageList1.Images(0)
 
 
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If ImageList1.Images.Count > 0 Then
            imageNumber = imageNumber + 1
            If imageNumber = ImageList1.Images.Count Then
                imageNumber = 0
            End If
            PictureBox1.Image = ImageList1.Images(imageNumber)
        End If
 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Timer1.Enabled Then
            Timer1.Stop()
        Else
            Timer1.Start()
        End If
    End Sub
End Class

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24237502
The only thing I can think of is that somehow you are accessing the ImageList before it has been properly initialized.

Try using the Shown() event instead of the Load() event:
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' ...don't use this event...
    End Sub
 
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        PictureBox1.Image = ImageList1.Images(0)
    End Sub
 
End Class

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:pcmedic4u
ID: 24237615
That worked.  Thanks. Your getting all 500 points.

One more quick question. I have two buttons in the form. Once called Animate, the second called CHange Speed.

What code can I use to make the animation "speed up" everytime I hit the Animate button and what code can I use to "slow down" the animations when I hit the Change Speed button?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24237638
You would change the Interval() Property of your Timer.

To speed it up, make the Interval Smaller:

    If Timer1.Interval > 100 Then
        Timer1.Interval = Timer1.Interval - 100
    End If

Conversely, to slow it down, make it Larger:

    Timer1.Interval = Timer1.Interval + 100


0
 

Author Comment

by:pcmedic4u
ID: 24237958
I need to make the animation go faster and slower by the user clicking on the buttons.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24239167
Ok...so put the two very tiny snippets I posted above into the Click() handlers of your buttons!
0

Featured Post

Independent Software Vendors: 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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

807 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