SettingsForm.vb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. Public Class SettingsForm
  2. 'Variables for settings
  3. Public boolRandomize, boolTryAgain As Boolean
  4. Public iIncorrect, iCorrect, iArrayLength, iTestLength As Integer
  5. 'Array to hold the questions
  6. Public iQuestions(2, 0) As Integer
  7. Dim progress As New ProgressForm
  8. Private Sub cmdStart_Click(sender As System.Object, e As System.EventArgs) Handles cmdStart.Click
  9. readSettings()
  10. Try 'Catch exception if no table was selected
  11. createQuestions()
  12. Catch ex As Exception
  13. MsgBox("You must select at least one table!")
  14. Exit Sub
  15. End Try
  16. 'Do the test the desired amount of times
  17. 'Set maximum value for the prograssbar
  18. progress.ProgressBar2.Maximum = iTestLength
  19. Try
  20. For i = 1 To iTestLength
  21. If boolRandomize Then 'Shuffle the array?
  22. performTest(shuffleArray(iQuestions)) 'Shuffle and test
  23. Else
  24. performTest(iQuestions) 'Just test
  25. End If
  26. 'Update value on ProgressBar2
  27. progress.ProgressBar2.Value = i
  28. Next
  29. progress.Close()
  30. Me.Show()
  31. Catch ex As Exception 'Return to the settings form
  32. Me.Show()
  33. progress.Close()
  34. Exit Sub
  35. End Try
  36. 'Create and show the ResultsForm
  37. Dim pf As New ResultsForm
  38. pf.Show()
  39. End Sub
  40. Private Sub cbAlla_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles cbAll.CheckedChanged
  41. 'Set all checkboxes to checked or not
  42. If cbAll.Checked Then
  43. cb1.Checked = True
  44. cb2.Checked = True
  45. cb3.Checked = True
  46. cb4.Checked = True
  47. cb5.Checked = True
  48. cb6.Checked = True
  49. cb7.Checked = True
  50. cb8.Checked = True
  51. cb9.Checked = True
  52. cb10.Checked = True
  53. Else
  54. cb1.Checked = False
  55. cb2.Checked = False
  56. cb3.Checked = False
  57. cb4.Checked = False
  58. cb5.Checked = False
  59. cb6.Checked = False
  60. cb7.Checked = False
  61. cb8.Checked = False
  62. cb9.Checked = False
  63. cb10.Checked = False
  64. End If
  65. End Sub
  66. Private Sub performTest(uppgifter As Integer(,))
  67. 'Reset counters
  68. iIncorrect = 0
  69. iCorrect = 0
  70. Dim iUserAnswer, iCorrectAnswer As Integer
  71. Dim boolAlreadyGuessedWrong As Boolean 'Keeps track of if the user has already guessed wrong, so that no points will be awarded after having guessed wrong once
  72. 'Set progressbar max value
  73. progress.ProgressBar1.Maximum = iArrayLength
  74. 'Set label start value
  75. progress.Label1.Text = "1 / " & iArrayLength
  76. 'Show the progress window
  77. progress.Show()
  78. 'Hide settings-box
  79. Me.Hide()
  80. 'Go through the questions and get answers
  81. For i = 0 To iArrayLength - 1
  82. 'Calculate correct answer
  83. iCorrectAnswer = uppgifter((0), (i)) * uppgifter((1), (i))
  84. 'Try until a number is entered, and if the user selected, try again until the correct answer is given
  85. While True
  86. Try
  87. 'Ask for the answer
  88. Dim strAnswer As String = InputBox(uppgifter((0), (i)) & " X " & uppgifter((1), (i)))
  89. 'Allow the user to abort
  90. If strAnswer = "" Then
  91. Throw New OperationCanceledException
  92. End If
  93. iUserAnswer = strAnswer
  94. Catch ex1 As OperationCanceledException
  95. If MsgBox("Are you sure you want to cancel?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
  96. Throw ex1
  97. Else
  98. Continue While
  99. End If
  100. Catch ex As Exception 'Catch errors
  101. MsgBox("Only numbers please!")
  102. Continue While 'Try again
  103. End Try
  104. 'Check answer
  105. If iCorrectAnswer = iUserAnswer Then 'Correct
  106. 'If the user already guessed wrong, no points are awarded
  107. If Not boolAlreadyGuessedWrong Then
  108. iCorrect += 1
  109. End If
  110. boolAlreadyGuessedWrong = False 'Reset variable
  111. 'Next question
  112. Exit While
  113. Else 'Incorrect
  114. 'Try again or continue
  115. If boolTryAgain Then
  116. MsgBox("Wrong, try again!")
  117. 'If the user already guessed wrong, give no more incorrect points
  118. If Not boolAlreadyGuessedWrong Then
  119. iIncorrect += 1
  120. End If
  121. boolAlreadyGuessedWrong = True 'The user has been wrong
  122. 'Retry
  123. Continue While
  124. Else
  125. MsgBox("Wrong, the answer is: " & iCorrectAnswer)
  126. iIncorrect += 1
  127. 'Continue
  128. Exit While
  129. End If
  130. End If
  131. End While
  132. 'Update progress form
  133. progress.Label1.Text = i + 1 & " / " & iArrayLength 'Label
  134. progress.ProgressBar1.Value = i + 1 'ProgressBar
  135. Next
  136. End Sub
  137. Private Sub createQuestions()
  138. 'Reset the arraylength
  139. iArrayLength = 0
  140. 'Read all checkboxes and create questions
  141. If cb1.Checked Then
  142. For i = 1 To 10
  143. 'Redim array
  144. ReDim Preserve iQuestions(2, iArrayLength + 1)
  145. 'Add all values at the end
  146. iQuestions((0), (iArrayLength)) = 1
  147. iQuestions((1), (iArrayLength)) = i
  148. iArrayLength += 1
  149. Next
  150. End If
  151. If cb2.Checked Then
  152. For i = 1 To 10
  153. 'Redim array
  154. ReDim Preserve iQuestions(2, iArrayLength + 1)
  155. 'Add all values at the end
  156. iQuestions((0), (iArrayLength)) = 2
  157. iQuestions((1), (iArrayLength)) = i
  158. iArrayLength += 1
  159. Next
  160. End If
  161. If cb3.Checked Then
  162. For i = 1 To 10
  163. 'Redim array
  164. ReDim Preserve iQuestions(2, iArrayLength + 1)
  165. 'Add all values at the end
  166. iQuestions((0), (iArrayLength)) = 3
  167. iQuestions((1), (iArrayLength)) = i
  168. iArrayLength += 1
  169. Next
  170. End If
  171. If cb4.Checked Then
  172. For i = 1 To 10
  173. 'Redim array
  174. ReDim Preserve iQuestions(2, iArrayLength + 1)
  175. 'Add all values at the end
  176. iQuestions((0), (iArrayLength)) = 4
  177. iQuestions((1), (iArrayLength)) = i
  178. iArrayLength += 1
  179. Next
  180. End If
  181. If cb5.Checked Then
  182. For i = 1 To 10
  183. 'Redim array
  184. ReDim Preserve iQuestions(2, iArrayLength + 1)
  185. 'Add all values at the end
  186. iQuestions((0), (iArrayLength)) = 5
  187. iQuestions((1), (iArrayLength)) = i
  188. iArrayLength += 1
  189. Next
  190. End If
  191. If cb6.Checked Then
  192. For i = 1 To 10
  193. 'Redim array
  194. ReDim Preserve iQuestions(2, iArrayLength + 1)
  195. 'Add all values at the end
  196. iQuestions((0), (iArrayLength)) = 6
  197. iQuestions((1), (iArrayLength)) = i
  198. iArrayLength += 1
  199. Next
  200. End If
  201. If cb7.Checked Then
  202. For i = 1 To 10
  203. 'Redim array
  204. ReDim Preserve iQuestions(2, iArrayLength + 1)
  205. 'Add all values at the end
  206. iQuestions((0), (iArrayLength)) = 7
  207. iQuestions((1), (iArrayLength)) = i
  208. iArrayLength += 1
  209. Next
  210. End If
  211. If cb8.Checked Then
  212. For i = 1 To 10
  213. 'Redim array
  214. ReDim Preserve iQuestions(2, iArrayLength + 1)
  215. 'Add all values at the end
  216. iQuestions((0), (iArrayLength)) = 8
  217. iQuestions((1), (iArrayLength)) = i
  218. iArrayLength += 1
  219. Next
  220. End If
  221. If cb9.Checked Then
  222. For i = 1 To 10
  223. 'Redim array
  224. ReDim Preserve iQuestions(2, iArrayLength + 1)
  225. 'Add all values at the end
  226. iQuestions((0), (iArrayLength)) = 9
  227. iQuestions((1), (iArrayLength)) = i
  228. iArrayLength += 1
  229. Next
  230. End If
  231. If cb10.Checked Then
  232. For i = 1 To 10
  233. 'Redim array
  234. ReDim Preserve iQuestions(2, iArrayLength + 1)
  235. 'Add all values at the end
  236. iQuestions((0), (iArrayLength)) = 10
  237. iQuestions((1), (iArrayLength)) = i
  238. iArrayLength += 1
  239. Next
  240. End If
  241. Try
  242. 'Try to get the first value, if there is none, throw exception
  243. Dim temp As Integer = iQuestions(0, 1)
  244. Catch ex As Exception
  245. Throw ex
  246. End Try
  247. 'Remove the last position
  248. ReDim Preserve iQuestions(2, iArrayLength - 1)
  249. End Sub
  250. Private Function shuffleArray(oldArray As Integer(,)) As Integer(,) 'Shuffle the array, and return the shuffle one
  251. Dim r As New Random 'Randomizer
  252. Dim newArray(2, iArrayLength) As Integer 'The new, shuffled array
  253. Dim iTagna(iArrayLength) As Integer 'Keeping track of which positions are already taken
  254. For i = 0 To iArrayLength - 1 'Go through the entire array
  255. While True 'Try again until Exit While
  256. Dim randomnummer As Integer = r.Next(0, iArrayLength)
  257. If iTagna(randomnummer) = 1 Then 'Check that the position is not taken
  258. Continue While 'Try again
  259. Else
  260. newArray((0), (i)) = oldArray((0), (randomnummer)) 'First factor
  261. newArray((1), (i)) = oldArray((1), (randomnummer)) 'Second factor
  262. iTagna(randomnummer) = 1 'Mark the position as taken
  263. Exit While 'Stop trying
  264. End If
  265. End While
  266. Next
  267. Return newArray
  268. End Function
  269. Private Sub readSettings() 'Reads settings from the form
  270. boolRandomize = rbRandom.Checked
  271. boolTryAgain = rbTryAgain.Checked
  272. 'Test length
  273. If rbLong.Checked Then
  274. iTestLength = 3 'All questions trice
  275. ElseIf rbMedium.Checked Then
  276. iTestLength = 2 'All questions twice
  277. Else
  278. iTestLength = 1 'All questions once
  279. End If
  280. End Sub
  281. End Class