none
System.IndexOutOfRangeException RRS feed

  • Question

  •  I running the program but come out this error. I cannot find how to fix it, can anyone help me?       

            int counter = 0;
            int select;
            public Form1()
            {
                InitializeComponent();
            }

            private void TeachingPage()
            {
                lblTitle.Visible = false;
                btnAnimal.Visible = false;
                btnFlower.Visible = false;
                btnInsect.Visible = false;
                btnStationery.Visible = false;
                btnShape.Visible = false;
                btnColour.Visible = false;
                btnFruit.Visible = false;
                btnVehicle.Visible = false;
                lblFileName.Visible = true;
                pictureBox1.Visible = true;
            }

            private void MainPage()
            {
                lblTitle.Visible = true;
                btnAnimal.Visible = true;
                btnFlower.Visible = true;
                btnInsect.Visible = true;
                btnStationery.Visible = true;
                btnShape.Visible = true;
                btnColour.Visible = true;
                btnFruit.Visible = true;
                btnVehicle.Visible = true;
                lblFileName.Visible =false;
                pictureBox1.Visible = false;
                counter = 0;
                SpeakCancel();
                timer1.Stop();
            }

            private void Speak()
            {
                SpeechSynthesizer s = new SpeechSynthesizer();
                s.SelectVoiceByHints(VoiceGender.Male);
                s.SpeakAsync(lblFileName.Text);
                s.SpeakAsync(lblFileName.Text);
            }

            private void SpeakCancel()
            {
                SpeechSynthesizer s = new SpeechSynthesizer();
                s.SelectVoiceByHints(VoiceGender.Male);
                s.SpeakAsyncCancelAll();
            }

            private void btnAnimal_Click(object sender, EventArgs e)
            {
                TeachingPage();
                counter++;
                string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Animal";
                string[] filePaths = Directory.GetFiles(path, "*.jpg");

                pictureBox1.Image = Image.FromFile(filePaths[counter]);
                lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]); //show name of the image
                Speak();

                timer1.Start();
                select = 1;
            }

            private void btnInsect_Click(object sender, EventArgs e)
            {
                TeachingPage();
                counter++;
                string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Insect";
                string[] filePaths = Directory.GetFiles(path, "*.jpg");

                pictureBox1.Image = Image.FromFile(filePaths[counter]);
                lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]); //show name of the image
                Speak();

                timer1.Start();
                select = 2;
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                counter++;
                if (select == 1)
                {
                    string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Animal";
                    string[] filePaths = Directory.GetFiles(path, "*.jpg");

                    if (counter < filePaths.Length)
                    {
                        pictureBox1.Image = Image.FromFile(filePaths[counter]);
                        lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]);
                        Speak();
                    }
                    else
                    {
                        MainPage();                   
                    }
                }
                else if (select == 2)
                {
                    string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Insect";
                    string[] filePaths = Directory.GetFiles(path, "*.jpg");

                    if (counter < filePaths.Length)
                    {
                        pictureBox1.Image = Image.FromFile(filePaths[counter]);
                        lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]);
                        Speak();
                    }
                    else
                    {
                        MainPage();
                    }
                }
            }        
        }


    Friday, November 3, 2017 2:38 AM

Answers

  • Obviously counter is larger than the number of files in the insect folder.

    One possibility is that filePaths is empty because you have the wrong folder for the insect pictures or because there are no pictures in the correct folder.

    Another possibility is that, because you are using the same counter for animals and insects, you have clicked the animals button first which has run and set counter to value larger that the number of insect pictures, and you have not reset it.

    To fix both of these possibilities, you should check that counter is in range and also reset it at the start of the method (instead of incrementing it with counter++).

            private void btnInsect_Click(object sender, EventArgs e)
            {
                TeachingPage();
                counter = 0; // Reset to zero instead of increment. So we start with the first image in the list.
                string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Insect";
                string[] filePaths = Directory.GetFiles(path, "*.jpg");
    
                if(counter < filePaths.Length) // In case filePaths has zero entries.
                {
                   pictureBox1.Image = Image.FromFile(filePaths[counter]);
                   lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]); //show name of the image
                   Speak();
    
                   timer1.Start();
                }
                select = 2;
            }

    • Marked as answer by Weishen0130 Friday, November 3, 2017 4:54 AM
    Friday, November 3, 2017 4:47 AM

All replies

  • Which line is the error on?

    And by the way, you should insert code using the toolbar button, not like ordinary text.

    Friday, November 3, 2017 3:42 AM
  • pictureBox1.Image = Image.FromFile(filePaths[counter]);

    it happens when i click the btnInsect,but nothing happens when I click the btn Animal

    Friday, November 3, 2017 4:33 AM
  • Obviously counter is larger than the number of files in the insect folder.

    One possibility is that filePaths is empty because you have the wrong folder for the insect pictures or because there are no pictures in the correct folder.

    Another possibility is that, because you are using the same counter for animals and insects, you have clicked the animals button first which has run and set counter to value larger that the number of insect pictures, and you have not reset it.

    To fix both of these possibilities, you should check that counter is in range and also reset it at the start of the method (instead of incrementing it with counter++).

            private void btnInsect_Click(object sender, EventArgs e)
            {
                TeachingPage();
                counter = 0; // Reset to zero instead of increment. So we start with the first image in the list.
                string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Insect";
                string[] filePaths = Directory.GetFiles(path, "*.jpg");
    
                if(counter < filePaths.Length) // In case filePaths has zero entries.
                {
                   pictureBox1.Image = Image.FromFile(filePaths[counter]);
                   lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]); //show name of the image
                   Speak();
    
                   timer1.Start();
                }
                select = 2;
            }

    • Marked as answer by Weishen0130 Friday, November 3, 2017 4:54 AM
    Friday, November 3, 2017 4:47 AM
  • Thank you. It is caused by the counter. After reset the counter, the program is worked now.
    Friday, November 3, 2017 4:55 AM