none
I want to make a simple picture slideshow from the stated folder. When i press button1, there is no change in both label1 and picturebox1. RRS feed

  • Question



  •     int counter = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start(); //start the slideshow
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            counter++; 

            string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Animal";//directory of the picture

            string[] filePaths = Directory.GetFiles(path, "*.jpg ");


            if (counter > filePaths.Length - 1)
            {
                counter = 0;   //reset the picture
            }

            pictureBox1.Image = Image.FromFile(filePaths[counter]); //show image

            label1.Text = filePaths[counter]; //show name of the image
        }
    Thursday, November 2, 2017 2:01 AM

Answers

  • I have tried your code and it works. Can you run it under the debugger and see if it is finding the files correctly?

    If you don't know how to use the debugger, try adding some MessageBoxes to tell you what is going on. Here's an example.

       private void timer1_Tick(object sender, EventArgs e)
        {
            counter++; 
    
            string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Animal";//directory of the picture
    
            string[] filePaths = Directory.GetFiles(path, "*.jpg ");
    
            // A MessageBox for debugging.
            MessageBox.Show("Testing", "Number of files = " + filePaths.Length.ToString(), MessageBoxButtons.OK);
    
    
            if (counter > filePaths.Length - 1)
            {
                counter = 0;   //reset the picture
            }
    
            pictureBox1.Image = Image.FromFile(filePaths[counter]); //show image
    
            label1.Text = filePaths[counter]; //show name of the image
        }

    So in the above example, if the count appears to be zero, this will tell you that perhaps you have the wrong folder, or there aren't any jpg files in the folder like you think. If no MessageBox appears at all, that will tell you that you are not correctly subscribed to the method.


    • Edited by Ante Meridian Thursday, November 2, 2017 2:53 AM typo
    • Marked as answer by Weishen0130 Thursday, November 2, 2017 4:30 AM
    Thursday, November 2, 2017 2:53 AM

All replies

  • Are you sure the Timer's tick event is correctly subscribed to the timer1_Tick Method?

    Are you sure the Timer's interval is something reasonable?

    Something like so.

             timer.Tick += new EventHandler(timer1_Tick);
             timer.Interval = 1000; // One second.

    Thursday, November 2, 2017 2:20 AM
  • Thanks a lot. My timer's tick event is correctly subscribed to the time1_Tick Method now. The timer's interval is 2000ms. But there is still no changes in label and picturebox
    Thursday, November 2, 2017 2:32 AM
  • I have tried your code and it works. Can you run it under the debugger and see if it is finding the files correctly?

    If you don't know how to use the debugger, try adding some MessageBoxes to tell you what is going on. Here's an example.

       private void timer1_Tick(object sender, EventArgs e)
        {
            counter++; 
    
            string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Animal";//directory of the picture
    
            string[] filePaths = Directory.GetFiles(path, "*.jpg ");
    
            // A MessageBox for debugging.
            MessageBox.Show("Testing", "Number of files = " + filePaths.Length.ToString(), MessageBoxButtons.OK);
    
    
            if (counter > filePaths.Length - 1)
            {
                counter = 0;   //reset the picture
            }
    
            pictureBox1.Image = Image.FromFile(filePaths[counter]); //show image
    
            label1.Text = filePaths[counter]; //show name of the image
        }

    So in the above example, if the count appears to be zero, this will tell you that perhaps you have the wrong folder, or there aren't any jpg files in the folder like you think. If no MessageBox appears at all, that will tell you that you are not correctly subscribed to the method.


    • Edited by Ante Meridian Thursday, November 2, 2017 2:53 AM typo
    • Marked as answer by Weishen0130 Thursday, November 2, 2017 4:30 AM
    Thursday, November 2, 2017 2:53 AM
  • I found the mistake already. Thank you for your help.
    Thursday, November 2, 2017 3:55 AM
  • The label show the full location detail of the image, is that any method that the label show the picture name only? like 

    C:\Users\Tan Wei Shen\Desktop\FYP\Animal\cat.jpg

    it will show "cat" only.

    Thursday, November 2, 2017 4:33 AM
  • label1.Text = Path.GetFileNameWithoutExtension(filePaths[counter]);


    • Edited by Ante Meridian Thursday, November 2, 2017 4:59 AM wrong label name.
    Thursday, November 2, 2017 4:55 AM