Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 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 February 10th, 2005, 05:31 PM
Authorized User
 
Join Date: Jan 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default AllowPrintToFile

Can anyone tell me how to use the AllowPrintToFile property in PrintDialog. Thanks
 
Old February 11th, 2005, 12:30 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

When you print to a file, the stream of data that would have gone to the printer goes to a file instead. This stream includes all of the control codes, etc.

If you want to save data to a file through the print dialog, add a generic text printer driver, and select that printer in the print dialog, and the print to file checkbox. That way you'll get straight text into the file, spaced and so on so as to fit the page right.
 
Old February 15th, 2005, 02:03 PM
Authorized User
 
Join Date: Jan 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is it possible for me to get some sample codes? I just want to save my data from a select statement to a file and write the file out somewhere. Thanks.
 
Old February 15th, 2005, 02:42 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

That is a totally different question...

.NET has file-handling classes, methods and functions. You can use those to just write what you want to files.
I believe you can even use ADO.NET to treat a file in an organized fashion.

You probably will end up creating your own dialog to allow users to choose where to save the data, followed by using code to directly manipulate the file(s).
 
Old February 16th, 2005, 12:40 PM
Authorized User
 
Join Date: Jan 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Due to the kind of users I have, I do not think it will be a good idea to create a dialog to allow the user to specific where they want to save the file.

Is it possible for me to get some sample code to get started? I still have no idea where to start. Thanks.
 
Old February 16th, 2005, 04:43 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

From Visual Studio Help:

Basic File I/O
The abstract base class Stream supports reading and writing bytes. Stream integrates asynchronous support. Its default implementations define synchronous reads and writes in terms of their corresponding asynchronous methods, and vice versa.
   All classes that represent streams inherit from the Stream class. The Stream class and its derived classes provide a generic view of data sources and repositories, isolating the programmer from the specific details of the operating system and underlying devices.
Streams involve these fundamental operations:

ʉۢ Streams can be read from. Reading is the transfer of
   data from a stream into a data structure, such as an
   array of bytes.

ʉۢ Streams can be written to. Writing is the transfer of
   data from a data source into a stream.

ʉۢ Streams can support seeking. Seeking is the querying and
   modifying of the current position within a stream.

Depending on the underlying data source or repository, streams might support only some of these capabilities. For example, NetworkStreams do not support seeking. The CanRead, CanWrite, and CanSeek properties of Stream and its derived classes determine the operations that various streams support.

Classes Used for File I/O
   FileObject provides a representation of a text file. Use the FileObject class to perform most typical text file operations such as reading, writing, appending, copying, deleting, moving, or renaming. You can also use FileObject to examine and, in some cases, set file attributes, encoding, and path information.
   Directory provides static methods for creating, moving, and enumerating through directories and subdirectories. The DirectoryInfo class provides instance methods.
   DirectoryInfo provides instance methods for creating, moving, and enumerating through directories and subdirectories. The Directory class provides static methods.
   File provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of a FileStream. The FileInfo class provides instance methods.
   FileInfo provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of a FileStream. The File class provides static methods.
   FileStream supports random access to files through its Seek method. FileStream opens files synchronously by default, but supports asynchronous operation as well. File contains static methods, and FileInfo contains instance methods.
   FileSystemInfo is the abstract base class for FileInfo and DirectoryInfo.
   Path provides methods and properties for processing directory strings in a cross-platform manner.
   File, FileInfo, Path, Directory, and DirectoryInfo are sealed (in Microsoft VB, NotInheritable) classes. You can create new instances of these classes, but they cannot have derived classes.

Classes Used for Reading from and Writing to Streams
   BinaryReader and BinaryWriter read and write encoded strings and primitive data types from and to Streams.
   StreamReader reads characters from Streams, using Encoding to convert characters to and from bytes. StreamReader has a constructor that attempts to ascertain what the correct Encoding for a given Stream is, based on the presence of an Encoding-specific preamble, such as a byte order mark.
   StreamWriter writes characters to Streams, using Encoding to convert characters to bytes.
   StringReader reads characters from Strings. StringReader allows you to treat Strings with the same API, so your output can be either a Stream in any encoding or a String.
   StringWriter writes characters to Strings. StringWriter allows you to treat Strings with the same API, so your output can be either a Stream in any encoding or a String.
   TextReader is the abstract base class for StreamReader and StringReader. While the implementations of the abstract Stream class are designed for byte input and output, the implementations of TextReader are designed for Unicode character output.
   TextWriter is the abstract base class for StreamWriter and StringWriter. While the implementations of the abstract Stream class are designed for byte input and output, the implementations of TextWriter are designed for Unicode character input.
================================================== ====================
In Visual Studio Help:
Writing Text to a File
The following code example shows a simple way to write text to a text file:
Code:
Imports System
Imports System.IO

Class Test
    Public Shared Sub Main()
        ' Create an instance of StreamWriter to write text to a file.
        Dim sw As StreamWriter = New StreamWriter("TestFile.txt")

        ' Add some text to the file.
        sw.Write("This is the ")
        sw.WriteLine("header for the file.")
        sw.WriteLine("-------------------")

        ' Arbitrary objects can also be written to the file.
        sw.Write("The date is: ")
        sw.WriteLine(DateTime.Now)
        sw.Close()
    End Sub
End Class
The following code example creates a new text file and writes a string to it:
Code:
Option Explicit On 
Option Strict On
Imports System
Imports System.IO
Public Class TextToFile
    Private Const FILE_NAME As String = "MyFile.txt"
    Public Shared Sub Main()
        If File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} already exists.", FILE_NAME)
            Return
        End If
        Dim sr As StreamWriter = File.CreateText(FILE_NAME)
        sr.WriteLine("This is my file.")

        ' {0} and {1} (those are curly braces) in the fol. are replaceable
        ' parameters.  The arguments ‘1’ and ‘4.2’ will automatically be 
        ' put in their place.  Those arguments can be variables,  func-
        ' tions, recordset values, etc.
        sr.WriteLine("I can write ints {0} or floats {1}, and so on.", 1, 4.2)
        sr.Close()
    End Sub
End Class
Does this get you there?









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