Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am editing my previously posted question for clarity. I am trying to write a code where a new image is displayed every two seconds. I have written a code that randomly takes images from an imagelist and displays them. However, I need it to be in a sequential order. What is the best way to do this?

Ps. as you can probably tell, I am new to this site so I apologize for any formatting errors.

What I have tried:

C#
private void timer1_Tick(object sender, EventArgs e)
        {
            Random mySchool;
            mySchool = new Random();  
            picCompanies.Image = this.imageList1.Images[mySchool.Next(4)]; 
        }
Posted
Updated 4-Mar-18 13:37pm
v2
Comments
PIEBALDconsult 4-Mar-18 19:38pm    
P.S. Don't do Random like that -- it won't do what you want -- better to make it a static member of the class and instantiate it only once.

1 solution

You do it in the same way as with a random number, only now you use your 'own' number that is incremented each time it is used (there has to be at least 1 image in the list or you have to add more error checking):
C#
private int myNumber = 0;

private void timer1_Tick(object sender, EventArgs e)
{
    picCompanies.Image = this.imageList1.Images[myNumber];
    myNumber = myNumber + 1;
    if (myNumber >= imageList1.Images.Count) myNumber = 0;
}
 
Share this answer
 
Comments
PIEBALDconsult 4-Mar-18 19:42pm    
Or
myNumber = ( myNumber + 1 ) % imageList1.Images.Count
[no name] 4-Mar-18 19:56pm    
Nice! But probably a little too complicated given the level of this question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900