By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Question on where would I put a call to subroutine

P: n/a
I have a app I have written that when the program is opened it logs out to a
logfile. What I can't figure out is that "X" button at the top. If a user
clicks on that instead of my menu item close how can I call the log sub and
write a closed program log entry ?
'Add any initialization after the InitializeComponent() call
CreateLFile()

Dim myear As String = Date.Now.Year.ToString
Dim mdate As String = Date.Now.Day.ToString
Dim mmonth As String = Date.Now.Month.ToString
Dim mtime As String = Date.Now.Hour.ToString
Dim mmin As String = Date.Now.Minute.ToString
Dim filename As String = "c:\log\" + myear + "-" + mmonth + "-" + mdate
+ " " + mtime + "-" + mmin + "_log.txt"

Public Function CreateLFile()
Dim fLen As Integer, filepath As String
filepath = filename
On Error Resume Next
fLen = Len(Dir$(filepath))
If fLen = 0 Then
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter
oWrite = oFile.CreateText(filename)
oWrite.WriteLine("Opened new logfile " + filename + ".")
oWrite.Close()
Else
Dim w As StreamWriter = File.AppendText(filename)
Log("Reopened logfile.", w)
w.Close()
End If
End Function

Public Shared Sub Log(ByVal logMessage As String, ByVal w As TextWriter)
w.Write(ControlChars.CrLf & "Log Entry : ")
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString())
w.WriteLine(" :")
w.WriteLine(" :{0}", logMessage)
w.WriteLine("-------------------------------")
' Update the underlying file.
w.Flush()
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Dim w As StreamWriter = File.AppendText(filename)
Log("Program Closed.", w)
w.Close()
Close()
End Sub

Thanks in advance for any help offered on this :)
Mike
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Call your sub in the Form's "Unload" event handler.
"Michael R. Pierotti" <mr*@hafatel.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I have a app I have written that when the program is opened it logs out to
a
logfile. What I can't figure out is that "X" button at the top. If a user
clicks on that instead of my menu item close how can I call the log sub
and
write a closed program log entry ?
'Add any initialization after the InitializeComponent() call
CreateLFile()

Dim myear As String = Date.Now.Year.ToString
Dim mdate As String = Date.Now.Day.ToString
Dim mmonth As String = Date.Now.Month.ToString
Dim mtime As String = Date.Now.Hour.ToString
Dim mmin As String = Date.Now.Minute.ToString
Dim filename As String = "c:\log\" + myear + "-" + mmonth + "-" + mdate
+ " " + mtime + "-" + mmin + "_log.txt"

Public Function CreateLFile()
Dim fLen As Integer, filepath As String
filepath = filename
On Error Resume Next
fLen = Len(Dir$(filepath))
If fLen = 0 Then
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter
oWrite = oFile.CreateText(filename)
oWrite.WriteLine("Opened new logfile " + filename + ".")
oWrite.Close()
Else
Dim w As StreamWriter = File.AppendText(filename)
Log("Reopened logfile.", w)
w.Close()
End If
End Function

Public Shared Sub Log(ByVal logMessage As String, ByVal w As
TextWriter)
w.Write(ControlChars.CrLf & "Log Entry : ")
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString())
w.WriteLine(" :")
w.WriteLine(" :{0}", logMessage)
w.WriteLine("-------------------------------")
' Update the underlying file.
w.Flush()
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Dim w As StreamWriter = File.AppendText(filename)
Log("Program Closed.", w)
w.Close()
Close()
End Sub

Thanks in advance for any help offered on this :)
Mike

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.