tan shen

tan shen

  • NA
  • 11
  • 433

System.IndexOutOfRangeException

Nov 2 2017 10:53 PM
I running the program but come out this error. I cannot find how to fix it, can anyone help me?
  1. int counter = 0;  
  2. int select;  
  3. public Form1()  
  4. {  
  5. InitializeComponent();  
  6. }  
  7.   
  8. private void TeachingPage()  
  9. {  
  10. lblTitle.Visible = false;  
  11. btnAnimal.Visible = false;  
  12. btnFlower.Visible = false;  
  13. btnInsect.Visible = false;  
  14. btnStationery.Visible = false;  
  15. btnShape.Visible = false;  
  16. btnColour.Visible = false;  
  17. btnFruit.Visible = false;  
  18. btnVehicle.Visible = false;  
  19. lblFileName.Visible = true;  
  20. pictureBox1.Visible = true;  
  21. }  
  22.   
  23. private void MainPage()  
  24. {  
  25. lblTitle.Visible = true;  
  26. btnAnimal.Visible = true;  
  27. btnFlower.Visible = true;  
  28. btnInsect.Visible = true;  
  29. btnStationery.Visible = true;  
  30. btnShape.Visible = true;  
  31. btnColour.Visible = true;  
  32. btnFruit.Visible = true;  
  33. btnVehicle.Visible = true;  
  34. lblFileName.Visible =false;  
  35. pictureBox1.Visible = false;  
  36. counter = 0;  
  37. SpeakCancel();  
  38. timer1.Stop();  
  39. }  
  40.   
  41. private void Speak()  
  42. {  
  43. SpeechSynthesizer s = new SpeechSynthesizer();  
  44. s.SelectVoiceByHints(VoiceGender.Male);  
  45. s.SpeakAsync(lblFileName.Text);  
  46. s.SpeakAsync(lblFileName.Text);  
  47. }  
  48.   
  49. private void SpeakCancel()  
  50. {  
  51. SpeechSynthesizer s = new SpeechSynthesizer();  
  52. s.SelectVoiceByHints(VoiceGender.Male);  
  53. s.SpeakAsyncCancelAll();  
  54. }  
  55.   
  56. private void btnAnimal_Click(object sender, EventArgs e)  
  57. {  
  58. TeachingPage();  
  59. counter++;  
  60. string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Animal";  
  61. string[] filePaths = Directory.GetFiles(path, "*.jpg");  
  62.   
  63. pictureBox1.Image = Image.FromFile(filePaths[counter]);  
  64. lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]); //show name of the image  
  65. Speak();  
  66.   
  67. timer1.Start();  
  68. select = 1;  
  69. }  
  70.   
  71. private void btnInsect_Click(object sender, EventArgs e)  
  72. {  
  73. TeachingPage();  
  74. counter++;  
  75. string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Insect";  
  76. string[] filePaths = Directory.GetFiles(path, "*.jpg");  
  77.   
  78. pictureBox1.Image = Image.FromFile(filePaths[counter]);  
  79. lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]); //show name of the image  
  80. Speak();  
  81.   
  82. timer1.Start();  
  83. select = 2;  
  84. }  
  85.   
  86. private void timer1_Tick(object sender, EventArgs e)  
  87. {  
  88. counter++;  
  89. if (select == 1)  
  90. {  
  91. string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Animal";  
  92. string[] filePaths = Directory.GetFiles(path, "*.jpg");  
  93.   
  94. if (counter < filePaths.Length)  
  95. {  
  96. pictureBox1.Image = Image.FromFile(filePaths[counter]);  
  97. lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]);  
  98. Speak();  
  99. }  
  100. else  
  101. {  
  102. MainPage();  
  103. }  
  104. }  
  105. else if (select == 2)  
  106. {  
  107. string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Insect";  
  108. string[] filePaths = Directory.GetFiles(path, "*.jpg");  
  109.   
  110. if (counter < filePaths.Length)  
  111. {  
  112. pictureBox1.Image = Image.FromFile(filePaths[counter]);  
  113. lblFileName.Text = Path.GetFileNameWithoutExtension(filePaths[counter]);  
  114. Speak();  
  115. }  
  116. else  
  117. {  
  118. MainPage();  
  119. }  
  120. }  
  121. }  
  122. }

Answers (4)