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.
source share