auto generate & save text file
Hi
I'm just starting to learn VP 2005 Express, i'm currently working an application that the end result is to WRITE the text from TXTBOX to a file then PRINT and SAVE to a file, i got the WRITE & PRINT part correctly but i cant get it to automatically generate a text file name and save it to a folder. For example when i click print, after it print i want it to save the file as "{"time"&"date"}.txt" so that it doesn't override the old one.
Please help me i'm realy stuck.
Here is my code:
Dim docFile As String = "C:\doc.txt"
Private Sub tbPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbPrint.Click
If tbDisplay.Text = Nothing Then
MsgBox("Click On New Document Button First")
ElseIf tbCal.Text = Nothing Then
MsgBox("Must complete Document to Print")
Else
tbDisplay.Text = tbDisplay.Text & vbCrLf & "Document: " & tbCal.Text & vbCrLf
If System.IO.File.Exists(docFile) = True Then
Dim objWriter As New System.IO.StreamWriter(docFile)
objWriter.WriteLine(tbDisplay.Text)
objWriter.Close()
Else
MsgBox("File Does Not Exist")
End If
Try
streamToPrint = New StreamReader("C:\doc.txt")
Try
printFont = New Font("Courier New", 8)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
tbDisplay.Clear()
tbCal.Clear()
End If
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single
Dim topMargin As Single
Dim line As String = Nothing
' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
' Print each line of the file.
While count < linesPerPage
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count += 1
End While
' If more lines exist, print another page.
If (line IsNot Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
End Class
|