Im sort of getting there with it now, i think, just wondering can anyone tell me what is wrong with this now? its full of things going wrong!
Option Strict On
Option Explicit On
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents dlgSave As System.Windows.Forms.SaveFileDialog
Friend WithEvents dlgOpen As System.Windows.Forms.OpenFileDialog
Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
Friend WithEvents MnuOpen As System.Windows.Forms.MenuItem
Friend WithEvents MnuFile As System.Windows.Forms.MenuItem
Friend WithEvents MnuSave As System.Windows.Forms.MenuItem
Friend WithEvents mnuMain As System.Windows.Forms.MainMenu
Friend WithEvents lblTemp As System.Windows.Forms.Label
Friend WithEvents lblDate As System.Windows.Forms.Label
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents btnEnter As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtTemp As System.Windows.Forms.TextBox
Friend WithEvents btnDelete As System.Windows.Forms.Button
Friend WithEvents MnuRec1 As System.Windows.Forms.MenuItem
Friend WithEvents MnuRec2 As System.Windows.Forms.MenuItem
Friend WithEvents MnuRec3 As System.Windows.Forms.MenuItem
Friend WithEvents MnuRec4 As System.Windows.Forms.MenuItem
Friend WithEvents lblFile As System.Windows.Forms.Label
Friend WithEvents MnuFileSep2 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtTemp = New System.Windows.Forms.TextBox
Me.lblTemp = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.dlgSave = New System.Windows.Forms.SaveFileDialog
Me.dlgOpen = New System.Windows.Forms.OpenFileDialog
Me.mnuMain = New System.Windows.Forms.MainMenu
Me.MnuFile = New System.Windows.Forms.MenuItem
Me.MnuOpen = New System.Windows.Forms.MenuItem
Me.MnuSave = New System.Windows.Forms.MenuItem
Me.MenuItem4 = New System.Windows.Forms.MenuItem
Me.MnuRec1 = New System.Windows.Forms.MenuItem
Me.MnuRec2 = New System.Windows.Forms.MenuItem
Me.MnuRec3 = New System.Windows.Forms.MenuItem
Me.MnuRec4 = New System.Windows.Forms.MenuItem
Me.lblDate = New System.Windows.Forms.Label
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.btnEnter = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.btnDelete = New System.Windows.Forms.Button
Me.lblFile = New System.Windows.Forms.Label
Me.MnuFileSep2 = New System.Windows.Forms.MenuItem
Me.SuspendLayout()
'
'txtTemp
'
Me.txtTemp.Location = New System.Drawing.Point(136, 56)
Me.txtTemp.Name = "txtTemp"
Me.txtTemp.TabIndex = 8
Me.txtTemp.Text = ""
'
'lblTemp
'
Me.lblTemp.Location = New System.Drawing.Point(48, 48)
Me.lblTemp.Name = "lblTemp"
Me.lblTemp.Size = New System.Drawing.Size(72, 32)
Me.lblTemp.TabIndex = 10
Me.lblTemp.Text = "Highest Temperature"
Me.lblTemp.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(64, 16)
Me.Label1.TabIndex = 7
Me.Label1.Text = "Entry Date"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'dlgSave
'
Me.dlgSave.Filter = "Text Files (*.txt)|*.txt"
Me.dlgSave.Title = "Save"
'
'dlgOpen
'
Me.dlgOpen.Filter = "Text Files (*.txt)|*.txt"
Me.dlgOpen.Title = "Open"
'
'mnuMain
'
Me.mnuMain.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MnuFile})
'
'MnuFile
'
Me.MnuFile.Index = 0
Me.MnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MnuOpen, Me.MnuSave, Me.MenuItem4, Me.MnuRec1, Me.MnuRec2, Me.MnuRec3, Me.MnuRec4, Me.MnuFileSep2})
Me.MnuFile.Text = "File"
'
'MnuOpen
'
Me.MnuOpen.Index = 0
Me.MnuOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO
Me.MnuOpen.Text = "Open"
'
'MnuSave
'
Me.MnuSave.Index = 1
Me.MnuSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS
Me.MnuSave.Text = "Save"
'
'MenuItem4
'
Me.MenuItem4.Index = 2
Me.MenuItem4.Text = "-"
'
'MnuRec1
'
Me.MnuRec1.Index = 3
Me.MnuRec1.Text = ""
Me.MnuRec1.Visible = False
'
'MnuRec2
'
Me.MnuRec2.Index = 4
Me.MnuRec2.Text = ""
Me.MnuRec2.Visible = False
'
'MnuRec3
'
Me.MnuRec3.Index = 5
Me.MnuRec3.Text = ""
Me.MnuRec3.Visible = False
'
'MnuRec4
'
Me.MnuRec4.Index = 6
Me.MnuRec4.Text = ""
Me.MnuRec4.Visible = False
'
'lblDate
'
Me.lblDate.Location = New System.Drawing.Point(96, 8)
Me.lblDate.Name = "lblDate"
Me.lblDate.Size = New System.Drawing.Size(100, 16)
Me.lblDate.TabIndex = 12
Me.lblDate.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point(16, 128)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.ScrollAlwaysVisible = True
Me.ListBox1.Size = New System.Drawing.Size(280, 121)
Me.ListBox1.TabIndex = 14
'
'btnEnter
'
Me.btnEnter.Location = New System.Drawing.Point(160, 88)
Me.btnEnter.Name = "btnEnter"
Me.btnEnter.TabIndex = 15
Me.btnEnter.Text = "Enter"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(248, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(32, 16)
Me.Label2.TabIndex = 13
Me.Label2.Text = "De C"
'
'btnDelete
'
Me.btnDelete.Location = New System.Drawing.Point(56, 88)
Me.btnDelete.Name = "btnDelete"
Me.btnDelete.TabIndex = 16
Me.btnDelete.Text = "Delete"
'
'lblFile
'
Me.lblFile.Location = New System.Drawing.Point(216, 8)
Me.lblFile.Name = "lblFile"
Me.lblFile.TabIndex = 17
'
'MnuFileSep2
'
Me.MnuFileSep2.Index = 7
Me.MnuFileSep2.Text = "-"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(320, 271)
Me.Controls.Add(Me.lblFile)
Me.Controls.Add(Me.btnDelete)
Me.Controls.Add(Me.btnEnter)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.lblDate)
Me.Controls.Add(Me.txtTemp)
Me.Controls.Add(Me.lblTemp)
Me.Controls.Add(Me.Label1)
Me.Menu = Me.mnuMain
Me.Name = "Form1"
Me.Text = "Temperatures"
Me.ResumeLayout(False)
End Sub
#End Region
Dim CurrentFile As String 'To keep track of the file we are working on.
Dim LastDateEntered As Date 'To know the last date entered to stop duplicates.
Dim rFiles(3) As String
Dim Arr(3) As MenuItem
Private Function AppPath() As String
Dim A As String
'Executing file location and name
A = Reflection.Assembly.GetExecutingAssembly.Location
'strip off file name
A = Mid(A, 1, InStrRev(A, "\"))
Return (A)
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblDate.Text = Date.Today.ToShortDateString
'check if a 'recent file' doc already exists
If System.IO.File.Exists(AppPath() + "recent.txt") Then
FillMenu()
Else
System.IO.File.Create(AppPath() + "recent.txt")
FillMenu()
End If
End Sub
Sub FillMenu()
Dim i As Integer
Dim info As String
Arr(0) = MnuRec1
Arr(1) = MnuRec2
Arr(2) = MnuRec3
Arr(3) = MnuRec4
FileOpen(2, AppPath() + "recent.txt", OpenMode.Input)
For i = 0 To 3
Input(2, info)
Arr(i).Text = info.ToString
Next
FileClose(2)
'create exit option as last item in FILE menu
Dim MnuExit As New MenuItem
MnuExit.Text = "E&xit"
MnuExit.Shortcut = Shortcut.AltF4
AddHandler MnuExit.Click, AddressOf MnuExit_Click
MnuFile.MenuItems.Add(MnuExit)
End Sub
Private Sub MnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If MessageBox.Show("Are you sure you wish to exit?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
'This line only write one line to the file recreating the earlier problem.
'WriteLine(2, CurrentFile.ToString)
Dim i As Integer
FileOpen(1, AppPath() & "/recent.txt", OpenMode.Output)
For i = 0 To 3
WriteLine(1, rFiles(i))
Next i
FileClose(1)
End If
Me.Close()
End Sub
Public Function DegCToF(ByVal c As Double) As Double
Return (c * 1.8) + 32
End Function
Private Sub MnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuOpen.Click
Dim TempF As Double
Try
'Call your file open routine passing it the filename
If dlgOpen.ShowDialog() = DialogResult.OK Then
'If a file has been selected open it.
FileOpen(1, dlgOpen.FileName, OpenMode.Input)
'Keep track of the file that has been opened
CurrentFile = dlgOpen.FileName
'Now loop through it and read the values into the listbox
Do Until EOF(1)
Dim dte As Date
Dim dta As String
Input(1, dte) 'date
Input(1, dta) 'data (temperatures)
LastDateEntered = dte 'What was the last date entered
'The below is adding todays date + the value of the textbox which is nothing.
'ListBox1.Items.Add(Now.ToShortDateString + " - " + txtTemp.Text + " Deg C" + " - " + " " + TempF.ToString + " Deg F")
'Should be
ListBox1.Items.Add(dte.ToShortDateString + " - " + dta + " Deg C" + " - " + " " + DegCToF(CDbl(dta)).ToString + " Deg F")
Loop
Call RearrangeFiles(dlgOpen.FileName)
End If
'NB CORRECTED CATCHES
Catch IOExcept As IO.FileNotFoundException
MessageBox.Show("The file has not been found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch MyException As Exception
MessageBox.Show("An error was detected. Please check, and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
FileClose(1)
End Try
End Sub
Private Sub MnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuSave.Click
Dim i As Integer
Dim Message As String
Try
'Open the file we have been working on.
FileOpen(1, CurrentFile, OpenMode.Output)
Message = ("The document has been saved to " + ControlChars.Cr)
Message += CurrentFile.ToString + ControlChars.Cr
MessageBox.Show(Message, "Save Completed", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
For i = 0 To ListBox1.Items.Count - 1
'Get the values from the listbox
Dim arr() As String = Split(ListBox1.Items(i).ToString, " ")
'Because we have altered the text in the listbox we need to alter
'the way we extract it.
WriteLine(1, arr(0).Trim, arr(4).Trim)
Next i
Catch MyException As IO.IOException
MessageBox.Show("An error has occured, and the Temperatures program needs to close.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
Me.Close()
Finally
FileClose(1)
End Try
End Sub
Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
'Use the function
'Dim TempC As String = txtTemp.Text.ToString
'Dim TempF As Double
'TempF = (CInt(TempC) * 1.8) + 32
If Now.Date = LastDateEntered Then
If MessageBox.Show("You have already entered information for today. Would you like to override the existing value? If not, the application will end.", "Error", _
MessageBoxButtons.YesNo, MessageBoxIcon.Error) = DialogResult.Yes Then
ListBox1.Items.RemoveAt(ListBox1.Items.Count - 1)
Else
Me.Close()
End If
End If
If CInt(txtTemp.Text) >= 99 Or CInt(txtTemp.Text) <= -20 Then
MessageBox.Show("Please enter a realistic temperature!", "Invalid Temperature", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
txtTemp.Text = ""
Else
'Enter the new values
ListBox1.Items.Add(Now.ToShortDateString + " - " + txtTemp.Text + " Deg C" + _
" - " + " " + DegCToF(CDbl(txtTemp.Text)).ToString + " Deg F")
txtTemp.Text = ""
LastDateEntered = Now.Date 'Update the variable
End If
End Sub
Private Sub txtSold_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTemp.KeyPress
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
'Allow numbers and backspace.
Case Asc("0") To Asc("9"), Asc(ControlChars.Back)
e.Handled = False
'Allow minus sign only if first entry.
Case Asc("-")
If Len(txtTemp.Text) = 0 Then
e.Handled = False
Else
e.Handled = True
End If
'Allow one decimal point
Case Asc(".")
If InStr(txtTemp.Text, ".") = 0 Then
e.Handled = False
Else
e.Handled = True
End If
'Disallow everything else
Case Else
e.Handled = True
End Select
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
ListBox1.Items.Clear()
End Sub
Private Sub RearrangeFiles(ByVal F As String)
'Reorder the four most recent files located in the menu structure
Dim TempArray(4) As String
Dim TempArrayCounter As Integer
Dim i As Integer
'The first item is the name of the file just opened.
TempArray(0) = F
TempArrayCounter = 1
'Loop the the origonal array, if the item is not the
For i = 0 To 3
If rFiles(i) <> F Then
TempArray(TempArrayCounter) = rFiles(i)
TempArrayCounter += 1
End If
Next i
'Read the temp array back into the origonal and the
'origonal back into the menu.
For i = 0 To 3
rFiles(i) = TempArray(i)
If rFiles(i) <> "" Then
Arr(i).Visible = True
Arr(i).Text = rFiles(i)
MnuFileSep2.Visible = True
End If
Next i
End Sub
End Class