How to switch between photos in windows using a time interval?

I have a problem while trying to switch between a group of photos in form (1). I use picturebox.Image to view the selected image, and after a certain period of time (say 4Sec) switch to a random photo in the same group of photos.

When switching between each photo, I would like to show a different form (2) for 1Sec, and then return to form (1).

my code in the form (1):

public partial class Form1: Form { public static Timer time; public static Form mod; public Form1() { InitializeComponent(); time = new Timer(); mod = new Form2(); mod.Owner = this; mod.Show(); this.Hide(); RunForm1(); } public void RunForm1() { for (int i = 0; i < groupSize; i++) { mod.Owner = this; mod.Show(); this.Hide(); } } } 

my code in the form (2):

 public partial class Form2: Form { public static Timer time; public int index = -1; public List<Image> images; public DirectoryInfo dI; public FileInfo[] fileInfos; public Form2() { InitializeComponent(); images = new List<Image>(); time = new Timer(); dI = new DirectoryInfo(@"C:\Users\Documents\Pictures"); fileInfos = dI.GetFiles("*.jpg", SearchOption.TopDirectoryOnly); foreach (FileInfo fi in fileInfos) images.Add(Image.FromFile(fi.FullName)); index = images.Count; time.Start(); RunForm2(); } public void RunForm2() { Random rand = new Random(); int randomCluster = rand.Next(0, 1); while (index != 0) { pictureBox1.Image = images[Math.Abs(index * randomCluster)]; setTimer(); index--; } } public void setTimer() { if (time.Interval == 4000) { this.Owner.Show(); this.Close(); } } 

}

My main problems in this code are: 1. Time is not updated, I mean, time.Interval is always set to 100 2. I don’t know why, but photos never appear in picturebox.Image, although in debug mode it shows that the photos are selected correctly.

Thank you for helping! Roy.

+4
source share
1 answer

You need to use the Tick event from the timer to find out when the time has passed. you check if the interval (==) is equal to 4000, but you need to set it to 4000 ( time.Interval = 4000 ) and then start the timer. Then the Tick event will fire after 4 seconds. And the problem of the image, which is not displayed, can be solved by calling pictureBox1.UpdateLayout() ;

+1
source

Source: https://habr.com/ru/post/1441770/


All Articles