Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 5th, 2007, 01:43 AM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Save PDF file as text file in VB.Net kvenkatu Classic ASP Basics 0 April 7th, 2006 01:09 PM
save a PDF file as text file through VB. NET kvenkatu VB.NET 0 April 6th, 2006 12:15 PM
How to Read,Write, & Save (File Processing) ck C++ Programming 2 April 18th, 2005 07:28 AM
how to generate a text file using .NET? pooh2323 VS.NET 2002/2003 2 February 11th, 2004 11:30 AM
How to generate a text file? bebe XML 2 October 9th, 2003 10:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.