1. Option Explicit On
  2. Option Strict On
  3.  
  4. Imports System.Convert
  5. Imports System.IO
  6. Imports System.Text.RegularExpressions
  7.  
  8.  
  9.  
  10. Public Class Form1
  11. Dim CurrentWeather() As Weather
  12. Dim DateIndex() As _DateIndex
  13. Dim DropDown() As String
  14. Dim Read As String
  15. Dim Write As String
  16. Dim _Match As String
  17.  
  18.  
  19.  
  20. Structure Weather
  21. Dim strDate As String
  22. Dim Temp As Double
  23. End Structure
  24.  
  25. Structure _DateIndex
  26. Dim _strDate As String
  27. Dim _Junk As String
  28. End Structure
  29.  
  30. Private Sub ExitButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButt.Click
  31. Me.Close()
  32. End Sub
  33.  
  34. Private Sub OpenButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenButt.Click
  35.  
  36. OpenFileDialog1.InitialDirectory = "C:\"
  37. OpenFileDialog1.FileName = "HandsOnProject1Data.txt"
  38. OpenFileDialog1.Filter = "Open Data File *.txt) | *.txt"
  39. OpenFileDialog1.ShowDialog()
  40.  
  41. If OpenFileDialog1.FileName <> Nothing Then
  42. Read = OpenFileDialog1.FileName
  43. ReadFile2(Read)
  44. End If
  45.  
  46. For i = 0 To DropDown.GetUpperBound(0)
  47. ComboDateBox.Items.Add(DropDown(i))
  48. Next
  49.  
  50. End Sub
  51.  
  52. Public Sub ReadFile(ByVal Path As String)
  53. Dim strReader As StreamReader
  54. Dim chrDelimiter() As Char = {ToChar(",")}
  55. Dim strLine As String
  56. Dim strFields() As String
  57. Dim index As Integer = 0
  58. Dim index2 As Integer = 0
  59. Dim t1 As String
  60. Dim t2 As String
  61. Dim MathAr() As Double
  62. Dim TempStrAr As New ArrayList
  63.  
  64. strReader = New StreamReader(Path)
  65. strLine = strReader.ReadLine()
  66. t1 = strLine.ToUpper
  67. t2 = _Match.ToUpper
  68.  
  69. Do Until strLine = Nothing
  70.  
  71. t1 = strLine.ToUpper
  72. If t1.Contains(t2) = True Then
  73.  
  74. If CurrentWeather Is Nothing Then
  75. ReDim CurrentWeather(index)
  76. Else
  77. index = CurrentWeather.GetUpperBound(0) + 1
  78. ReDim Preserve CurrentWeather(index)
  79. End If
  80.  
  81. strFields = strLine.Split(chrDelimiter)
  82.  
  83. CurrentWeather(index).strDate = strFields(0)
  84. CurrentWeather(index).Temp = CDbl(strFields(1))
  85.  
  86. index += 1
  87. End If
  88.  
  89. strLine = strReader.ReadLine()
  90.  
  91. Loop
  92.  
  93. 'DateBox.Items.Add(_Match) 'debugging tool
  94. 'TempBox.Items.Add(t1.Contains(t2)) 'debugging tool
  95.  
  96. For i = 0 To UBound(CurrentWeather)
  97. DateBox.Items.Add(CurrentWeather(i).strDate)
  98. TempBox.Items.Add(CurrentWeather(i).Temp)
  99. Next
  100.  
  101. For index2 = 0 To CurrentWeather.Count - 1
  102. TempStrAr.Add(CurrentWeather(index2).Temp)
  103. Next
  104.  
  105. index2 = 0
  106.  
  107. ReDim MathAr(TempStrAr.Count - 1)
  108.  
  109. For index2 = 0 To TempStrAr.Count - 1
  110. MathAr(index2) = CType(TempStrAr.Item(index2), Double)
  111. Next
  112.  
  113. AvgBox.Text = CType(Math.Round(MathAr.Average, 4), String)
  114. MinBox.Text = CType(MathAr.Min, String)
  115. MaxBox.Text = CType(MathAr.Max, String)
  116.  
  117.  
  118.  
  119. End Sub
  120.  
  121.  
  122. Public Sub ReadFile2(ByVal Path As String)
  123. Dim _strReader As StreamReader
  124. Dim _chrDelimiter() As Char = {ToChar(" ")}
  125. Dim _strLine As String
  126. Dim _strFields() As String
  127. Dim _index As Integer = 0
  128. Dim index2 As Integer = 0
  129. Dim TempAr As New ArrayList
  130. Dim TempAr2 As New ArrayList
  131.  
  132. _strReader = New StreamReader(Path)
  133. _strLine = _strReader.ReadLine()
  134.  
  135. Do Until _strLine = Nothing
  136.  
  137. If DateIndex Is Nothing Then
  138. ReDim DateIndex(_index)
  139. Else
  140. _index = DateIndex.GetUpperBound(0) + 1
  141. ReDim Preserve DateIndex(_index)
  142. End If
  143.  
  144. _strFields = _strLine.Split(_chrDelimiter)
  145.  
  146. DateIndex(_index)._strDate = _strFields(0)
  147. DateIndex(_index)._Junk = _strFields(1)
  148.  
  149. _strLine = _strReader.ReadLine()
  150. _index += 1
  151.  
  152. Loop
  153.  
  154. For index2 = 0 To DateIndex.Count - 1
  155. TempAr.Add(DateIndex(index2)._strDate)
  156. Next
  157.  
  158. index2 = 0
  159.  
  160. For index2 = 0 To TempAr.Count - 1
  161. If Not TempAr2.Contains(TempAr(index2)) Then
  162. TempAr2.Add(TempAr(index2))
  163. End If
  164. Next
  165.  
  166. index2 = 0
  167.  
  168. ReDim DropDown(TempAr2.Count - 1)
  169.  
  170. For index2 = 0 To TempAr2.Count - 1
  171. DropDown(index2) = CType(TempAr2.Item(index2), String)
  172. Next
  173.  
  174.  
  175. End Sub
  176.  
  177. Private Sub ComboDateBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboDateBox.SelectedValueChanged
  178.  
  179. DateBox.Items.Clear()
  180. TempBox.Items.Clear()
  181. Dim iclear As Integer = 0
  182.  
  183. If CurrentWeather Is Nothing Then
  184. Else
  185. Erase CurrentWeather
  186. End If
  187.  
  188. _Match = CType(ComboDateBox.SelectedItem, String)
  189. ReadFile(Read)
  190.  
  191. End Sub
  192.  
  193. Private Sub WriteButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WriteButt.Click
  194.  
  195. SaveFileDialog1.InitialDirectory = "C\:"
  196. SaveFileDialog1.FileName = "WeatherData.txt"
  197. SaveFileDialog1.Filter = "Save Data File *.txt) | *.txt"
  198. SaveFileDialog1.ShowDialog()
  199.  
  200. If SaveFileDialog1.FileName <> Nothing Then
  201. Write = SaveFileDialog1.FileName
  202. WriteFile(Write)
  203. End If
  204.  
  205. End Sub
  206.  
  207. Public Sub WriteFile(ByVal Path As String)
  208.  
  209. Dim WLine As StreamWriter
  210.  
  211. WLine = New StreamWriter(Path)
  212.  
  213. For index = 0 To CurrentWeather.Count - 1
  214. WLine.WriteLine(CurrentWeather(index).strDate & "," & CurrentWeather(index).Temp)
  215. Next
  216.  
  217. WLine.Close()
  218.  
  219. End Sub
  220. End Class
  221.