- •Using Your Sybex Electronic Book
- •Acknowledgments
- •Contents at a Glance
- •Introduction
- •Who Should Read This Book?
- •How About the Advanced Topics?
- •The Structure of the Book
- •How to Reach the Author
- •The Integrated Development Environment
- •The Start Page
- •Project Types
- •Your First VB Application
- •Making the Application More Robust
- •Making the Application More User-Friendly
- •The IDE Components
- •The IDE Menu
- •The Toolbox Window
- •The Solution Explorer
- •The Properties Window
- •The Output Window
- •The Command Window
- •The Task List Window
- •Environment Options
- •A Few Common Properties
- •A Few Common Events
- •A Few Common Methods
- •Building a Console Application
- •Summary
- •Building a Loan Calculator
- •How the Loan Application Works
- •Designing the User Interface
- •Programming the Loan Application
- •Validating the Data
- •Building a Math Calculator
- •Designing the User Interface
- •Programming the MathCalculator App
- •Adding More Features
- •Exception Handling
- •Taking the LoanCalculator to the Web
- •Working with Multiple Forms
- •Working with Multiple Projects
- •Executable Files
- •Distributing an Application
- •VB.NET at Work: Creating a Windows Installer
- •Finishing the Windows Installer
- •Running the Windows Installer
- •Verifying the Installation
- •Summary
- •Variables
- •Declaring Variables
- •Types of Variables
- •Converting Variable Types
- •User-Defined Data Types
- •Examining Variable Types
- •Why Declare Variables?
- •A Variable’s Scope
- •The Lifetime of a Variable
- •Constants
- •Arrays
- •Declaring Arrays
- •Initializing Arrays
- •Array Limits
- •Multidimensional Arrays
- •Dynamic Arrays
- •Arrays of Arrays
- •Variables as Objects
- •So, What’s an Object?
- •Formatting Numbers
- •Formatting Dates
- •Flow-Control Statements
- •Test Structures
- •Loop Structures
- •Nested Control Structures
- •The Exit Statement
- •Summary
- •Modular Coding
- •Subroutines
- •Functions
- •Arguments
- •Argument-Passing Mechanisms
- •Event-Handler Arguments
- •Passing an Unknown Number of Arguments
- •Named Arguments
- •More Types of Function Return Values
- •Overloading Functions
- •Summary
- •The Appearance of Forms
- •Properties of the Form Control
- •Placing Controls on Forms
- •Setting the TabOrder
- •VB.NET at Work: The Contacts Project
- •Anchoring and Docking
- •Loading and Showing Forms
- •The Startup Form
- •Controlling One Form from within Another
- •Forms vs. Dialog Boxes
- •VB.NET at Work: The MultipleForms Project
- •Designing Menus
- •The Menu Editor
- •Manipulating Menus at Runtime
- •Building Dynamic Forms at Runtime
- •The Form.Controls Collection
- •VB.NET at Work: The DynamicForm Project
- •Creating Event Handlers at Runtime
- •Summary
- •The TextBox Control
- •Basic Properties
- •Text-Manipulation Properties
- •Text-Selection Properties
- •Text-Selection Methods
- •Undoing Edits
- •VB.NET at Work: The TextPad Project
- •Capturing Keystrokes
- •The ListBox, CheckedListBox, and ComboBox Controls
- •Basic Properties
- •The Items Collection
- •VB.NET at Work: The ListDemo Project
- •Searching
- •The ComboBox Control
- •The ScrollBar and TrackBar Controls
- •The ScrollBar Control
- •The TrackBar Control
- •Summary
- •The Common Dialog Controls
- •Using the Common Dialog Controls
- •The Color Dialog Box
- •The Font Dialog Box
- •The Open and Save As Dialog Boxes
- •The Print Dialog Box
- •The RichTextBox Control
- •The RTF Language
- •Methods
- •Advanced Editing Features
- •Cutting and Pasting
- •Searching in a RichTextBox Control
- •Formatting URLs
- •VB.NET at Work: The RTFPad Project
- •Summary
- •What Is a Class?
- •Building the Minimal Class
- •Adding Code to the Minimal Class
- •Property Procedures
- •Customizing Default Members
- •Custom Enumerations
- •Using the SimpleClass in Other Projects
- •Firing Events
- •Shared Properties
- •Parsing a Filename String
- •Reusing the StringTools Class
- •Encapsulation and Abstraction
- •Inheritance
- •Inheriting Existing Classes
- •Polymorphism
- •The Shape Class
- •Object Constructors and Destructors
- •Instance and Shared Methods
- •Who Can Inherit What?
- •Parent Class Keywords
- •Derived Class Keyword
- •Parent Class Member Keywords
- •Derived Class Member Keyword
- •MyBase and MyClass
- •Summary
- •On Designing Windows Controls
- •Enhancing Existing Controls
- •Building the FocusedTextBox Control
- •Building Compound Controls
- •VB.NET at Work: The ColorEdit Control
- •VB.NET at Work: The Label3D Control
- •Raising Events
- •Using the Custom Control in Other Projects
- •VB.NET at Work: The Alarm Control
- •Designing Irregularly Shaped Controls
- •Designing Owner-Drawn Menus
- •Designing Owner-Drawn ListBox Controls
- •Using ActiveX Controls
- •Summary
- •Programming Word
- •Objects That Represent Text
- •The Documents Collection and the Document Object
- •Spell-Checking Documents
- •Programming Excel
- •The Worksheets Collection and the Worksheet Object
- •The Range Object
- •Using Excel as a Math Parser
- •Programming Outlook
- •Retrieving Information
- •Recursive Scanning of the Contacts Folder
- •Summary
- •Advanced Array Topics
- •Sorting Arrays
- •Searching Arrays
- •Other Array Operations
- •Array Limitations
- •The ArrayList Collection
- •Creating an ArrayList
- •Adding and Removing Items
- •The HashTable Collection
- •VB.NET at Work: The WordFrequencies Project
- •The SortedList Class
- •The IEnumerator and IComparer Interfaces
- •Enumerating Collections
- •Custom Sorting
- •Custom Sorting of a SortedList
- •The Serialization Class
- •Serializing Individual Objects
- •Serializing a Collection
- •Deserializing Objects
- •Summary
- •Handling Strings and Characters
- •The Char Class
- •The String Class
- •The StringBuilder Class
- •VB.NET at Work: The StringReversal Project
- •VB.NET at Work: The CountWords Project
- •Handling Dates
- •The DateTime Class
- •The TimeSpan Class
- •VB.NET at Work: Timing Operations
- •Summary
- •Accessing Folders and Files
- •The Directory Class
- •The File Class
- •The DirectoryInfo Class
- •The FileInfo Class
- •The Path Class
- •VB.NET at Work: The CustomExplorer Project
- •Accessing Files
- •The FileStream Object
- •The StreamWriter Object
- •The StreamReader Object
- •Sending Data to a File
- •The BinaryWriter Object
- •The BinaryReader Object
- •VB.NET at Work: The RecordSave Project
- •The FileSystemWatcher Component
- •Properties
- •Events
- •VB.NET at Work: The FileSystemWatcher Project
- •Summary
- •Displaying Images
- •The Image Object
- •Exchanging Images through the Clipboard
- •Drawing with GDI+
- •The Basic Drawing Objects
- •Drawing Shapes
- •Drawing Methods
- •Gradients
- •Coordinate Transformations
- •Specifying Transformations
- •VB.NET at Work: Plotting Functions
- •Bitmaps
- •Specifying Colors
- •Defining Colors
- •Processing Bitmaps
- •Summary
- •The Printing Objects
- •PrintDocument
- •PrintDialog
- •PageSetupDialog
- •PrintPreviewDialog
- •PrintPreviewControl
- •Printer and Page Properties
- •Page Geometry
- •Printing Examples
- •Printing Tabular Data
- •Printing Plain Text
- •Printing Bitmaps
- •Using the PrintPreviewControl
- •Summary
- •Examining the Advanced Controls
- •How Tree Structures Work
- •The ImageList Control
- •The TreeView Control
- •Adding New Items at Design Time
- •Adding New Items at Runtime
- •Assigning Images to Nodes
- •Scanning the TreeView Control
- •The ListView Control
- •The Columns Collection
- •The ListItem Object
- •The Items Collection
- •The SubItems Collection
- •Summary
- •Types of Errors
- •Design-Time Errors
- •Runtime Errors
- •Logic Errors
- •Exceptions and Structured Exception Handling
- •Studying an Exception
- •Getting a Handle on this Exception
- •Finally (!)
- •Customizing Exception Handling
- •Throwing Your Own Exceptions
- •Debugging
- •Breakpoints
- •Stepping Through
- •The Local and Watch Windows
- •Summary
- •Basic Concepts
- •Recursion in Real Life
- •A Simple Example
- •Recursion by Mistake
- •Scanning Folders Recursively
- •Describing a Recursive Procedure
- •Translating the Description to Code
- •The Stack Mechanism
- •Stack Defined
- •Recursive Programming and the Stack
- •Passing Arguments through the Stack
- •Special Issues in Recursive Programming
- •Knowing When to Use Recursive Programming
- •Summary
- •MDI Applications: The Basics
- •Building an MDI Application
- •Built-In Capabilities of MDI Applications
- •Accessing Child Forms
- •Ending an MDI Application
- •A Scrollable PictureBox
- •Summary
- •What Is a Database?
- •Relational Databases
- •Exploring the Northwind Database
- •Exploring the Pubs Database
- •Understanding Relations
- •The Server Explorer
- •Working with Tables
- •Relationships, Indices, and Constraints
- •Structured Query Language
- •Executing SQL Statements
- •Selection Queries
- •Calculated Fields
- •SQL Joins
- •Action Queries
- •The Query Builder
- •The Query Builder Interface
- •SQL at Work: Calculating Sums
- •SQL at Work: Counting Rows
- •Limiting the Selection
- •Parameterized Queries
- •Calculated Columns
- •Specifying Left, Right, and Inner Joins
- •Stored Procedures
- •Summary
- •How About XML?
- •Creating a DataSet
- •The DataGrid Control
- •Data Binding
- •VB.NET at Work: The ViewEditCustomers Project
- •Binding Complex Controls
- •Programming the DataAdapter Object
- •The Command Objects
- •The Command and DataReader Objects
- •VB.NET at Work: The DataReader Project
- •VB.NET at Work: The StoredProcedure Project
- •Summary
- •The Structure of a DataSet
- •Navigating the Tables of a DataSet
- •Updating DataSets
- •The DataForm Wizard
- •Handling Identity Fields
- •Transactions
- •Performing Update Operations
- •Updating Tables Manually
- •Building and Using Custom DataSets
- •Summary
- •An HTML Primer
- •HTML Code Elements
- •Server-Client Interaction
- •The Structure of HTML Documents
- •URLs and Hyperlinks
- •The Basic HTML Tags
- •Inserting Graphics
- •Tables
- •Forms and Controls
- •Processing Requests on the Server
- •Building a Web Application
- •Interacting with a Web Application
- •Maintaining State
- •The Web Controls
- •The ASP.NET Objects
- •The Page Object
- •The Response Object
- •The Request Object
- •The Server Object
- •Using Cookies
- •Handling Multiple Forms in Web Applications
- •Summary
- •The Data-Bound Web Controls
- •Simple Data Binding
- •Binding to DataSets
- •Is It a Grid, or a Table?
- •Getting Orders on the Web
- •The Forms of the ProductSearch Application
- •Paging Large DataSets
- •Customizing the Appearance of the DataGrid Control
- •Programming the Select Button
- •Summary
- •How to Serve the Web
- •Building a Web Service
- •Consuming the Web Service
- •Maintaining State in Web Services
- •A Data-Driven Web Service
- •Consuming the Products Web Service in VB
- •Summary
604 Chapter 13 WORKING WITH FOLDERS AND FILES
SW.WriteLine(itm.ToString)
Next
SW.Close()
FS.Close()
Notice that the Write method stores the item’s text, even though the item added to the control is an object. If you open the Items.txt file, you will read the following:
First Item {X=0,Y=0,Width=3,Height=3} {X=3,Y=4}
Last Item
Listing 13.17 clears the ListBox control and populates it again by reading the items from the file. (For more information on reading from a text file, see the discussion of the StreamReader object, in the following section.) Since the ListBox control’s items are stored as text, you can use the StreamWriter and StreamReader objects to write them to and read them from the file.
Listing 13.17: Reading an Items Collection From a Text File
Dim SR As StreamReader
Dim FS As FileStream
FS = New FileStream(“C:\Items.txt”, FileMode.Open) SR = New StreamReader(FS)
Dim itm As Object itm = SR.ReadLine()
While Not itm = Nothing ListBox1.Items.Add(itm) itm = SR.ReadLine()
End While
SR.Close()
FS.Close()
In the following sections, we’ll explore the BinaryWriter and BinaryReader objects, which are the equivalents of the StreamWriter and StreamReader objects for binary files. Because of the variety of the binary data types, these two objects provide many more methods than their text counterparts.
The BinaryWriter Object
To prepare your application to write to a binary file, you must set up a BinaryWriter object, with the statements shown here:
Dim BW As New BinaryWriter(FS)
where FS is a properly initialized FileStream object. You can also create a new BinaryWriter object directly on a file, with the following form of the constructor:
Dim BW As New StreamReader(path)
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
ACCESSING FILES 605
To specify the encoding of the text in the binary file, use the following form of the method:
Dim BW As New BinaryWriter(FS, encoding)
Dim BW As New BinaryWriter(path, encoding)
You can also specify a third argument with the size of the buffer to be used with the file input/output operations:
Dim BW As New BinaryWriter(FS, encoding, bufferSize)
Dim BW As New BinaryWriter(path, encoding, bufferSize)
Methods
The BinaryWriter object exposes the following methods for manipulating binary files.
Close
This method closes the current BinaryWriter and releases any system resources associated with it.
Flush
This method clears all buffers for the current writer and writes all buffered data to the underlying file.
Seek
This method sets the position within the current stream.
Write
This method writes a value to the current stream. This method is heavily overloaded, but it accepts a single argument, which is the value to be written to the file. The data type of its argument determines how it will be written. The Write method can save all the base types to the file except for the Date and Object types.
WriteString
Where all other data types can be written to a binary file with the Write method, strings must be written with the WriteString method. This method writes a length-prefixed string to the file and advances the current position by the appropriate number of bytes. The string is encoded by the current encoding scheme, and the default value is UTF8Encoding.
You will find examples of using the Write and WriteString methods of the BinaryWriter object at the end of the following section, which describes the methods of the BinaryReader object.
The BinaryReader Object
The BinaryReader object reads data from a binary file. As you have seen, binary files may also hold text, and the BinaryReader object provides the ReadString method to read strings written to the file
by the BinaryWriter.WriteString method.
To use the methods of the BinaryReader object in your code, you must first create an instance of the object. The BinaryReader object is associated with a FileStream object, and the simplest form of its constructor is
Dim BR As New BinaryReader(streamObj)
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
606 Chapter 13 WORKING WITH FOLDERS AND FILES
where streamObj is the FileStream object. You can also specify the character encoding scheme to be used with the BinaryReader object, using the following form of the constructor:
Dim BR As New BinaryReader(streamObj, encoding)
If you omit the encoding argument, the default UTF8Encoding will be used.
Methods
The BinaryReader object exposes the following methods for accessing the contents of a binary file.
Close
This method is the same as the Close method of the StreamReader object. It closes the current reader and releases the underlying stream.
PeekChar
This method returns the next available character from the stream without repositioning the current pointer. The character read is returned as an integer, or –1 if there are no more characters to be read from the stream. The name of the method doesn’t quite comply with the BinaryReader object, but here’s why. Peeking at the next byte makes sense only if the next byte is a character. Reading the first byte of a Double value, for example, wouldn’t help you much. A character is usually stored in a single byte (ASCII text), but it can also be stored in two bytes (Unicode text). The PeekChar method knows how many bytes it must read from the text (they’re determined by the current encoding), and it always returns a character, regardless of its size in bytes. The PeekChar method’s return value is an integer, not a character.
The Read Methods
The BinaryReader object exposes methods for reading the same base data types you can write to a file through the BinaryWriter object. Each method returns a value of the corresponding type (the ReadBoolean method returns a Boolean value, and so on) and only a single value of this type. To read multiple values of the same type, you must call the same method repeatedly. The various methods for reading the base data types from the file are briefly described in Table 13.7.
Table 13.7: The Read Methods of the BinaryReader Object
Value |
Effect |
ReadBoolean |
Reads and returns a True/False value. |
ReadByte |
Reads and returns a single byte. |
ReadBytes(byteArray, count) |
Reads and returns count bytes from the file and stores them into the Byte |
|
array passed as the first argument. |
ReadChar |
Reads and returns a character. Depending on how text was stored in the file, |
|
the ReadChar method may read one or two bytes (in the case of Unicode text) , |
|
but it always returns a character. |
|
Continued on next page |
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
|
ACCESSING FILES |
607 |
|
|
|
||
|
|
|
|
Table 13.7: The Read Methods of the BinaryReader Object (continued) |
|
|
|
Value |
Effect |
||
ReadChars(charArray, count) |
Reads and returns count characters from the file and stores them in the char- |
||
|
acter array specified as the first argument. |
||
ReadDecimal |
Reads and returns a Decimal value from the file. |
||
ReadDouble |
Reads and returns a Double value from the file. |
||
ReadInt16 |
Reads and returns a short Integer (a 2-byte) value. |
||
ReadInt32 |
Reads and returns an Integer (a 4-byte) value. |
||
ReadInt64 |
Reads and returns a Long Integer (8-byte) value. |
||
ReadSByte |
Reads and returns a signed byte. |
||
ReadSingle |
Reads a Single (4-byte) value from the file. |
||
ReadString |
Reads and returns a string from the file. The string must be stored in the file |
||
|
prefixed by its length. This is how the WriteString method stored strings to a |
||
|
text file, so there’s nothing you have to do anything special from within your |
||
|
code. If the string isn’t prefixed by its length, the ReadString method will |
||
|
read a string with the wrong number of characters. The method will inter- |
||
|
pret the first byte as the string’s length. |
||
ReadUInt16 |
Reads and returns an unsigned short Integer (2-byte) value. |
||
ReadUInt32 |
Reads and returns an unsigned Integer (4-byte) value |
||
ReadUInt64 |
Reads and returns an unsigned long Integer (8-byte) value |
||
|
|
|
|
To use these methods, you’re supposed to know the structure of the data stored in the file. A file with a price list, for example, contains the same items for each product. The first two fields are the product’s ID and description, followed by the product’s price, and other pieces of information, which are repeated for each product. Once you know the types of values stored in the file, you can call the appropriate methods to read the correct values. If you misread even a single value, none of the following values will be read correctly.
VB.NET at Work: The RecordSave Project
Let’s look at the code for saving structured information to a binary file. In this section, you’re going to build the RecordSave application, which demonstrates how to store a price list to a disk file and read it later from the same file. The main form of the application is shown in Figure 13.2. The Save Records button creates a few records and then saves them to disk. The Read Records button reads the records from the file and displays them on the ListBox control.
Each record of the price list contains the following fields:
The product’s ID (a String)
The product’s description (a String)
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
608Chapter 13 WORKING WITH FOLDERS AND FILES
The product’s price (a Single value)
The product’s availability (a Boolean value)
The minimum reorder quantity (an Integer value)
Figure 13.2
The RecordSave project demonstrates how to store records in a binary file.
The program saves each field as a separate entity, using the Write method of a BinaryStream object. Only the string is written to the file with the WriteString method, because we want to be able to read the string back with the ReadString method.
Since the price list contains many products, you will most likely store it in an array of custom Structures. The Product structure shown next is a simple, yet quite adequate, structure for our price list:
Structure Product
Dim ProdID As String
Dim prodDescription As String
Dim listPrice As Single
Dim available As Boolean
Dim minStock As Integer
End Structure
The code that writes the Structure to a binary file is shown in the Listing 13.18.
Listing 13.18: Saving a Record to a Binary File
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim BW As BinaryWriter
Dim FS As FileStream
FS = New FileStream(“Records.bin”, System.IO.FileMode.OpenOrCreate, _
System.IO.FileAccess.Write)
BW = New BinaryWriter(FS)
BW.BaseStream.Seek(0, SeekOrigin.Begin)
Dim p As New Product()
‘Save first record p.ProdID = “100-A39”
p.prodDescription = “Cellular Phone with built-in TV”
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
ACCESSING FILES 609
p.listPrice = 497.99 p.available = True p.minStock = 40 SaveRecord(BW, p)
‘Save second record p = New Product()
p.ProdID = “100-U300”
p.prodDescription = “Wireless Handheld” p.listPrice = 315.5
p.available = False p.minStock = 12 SaveRecord(BW, p)
‘Save third record
p = New Product() p.ProdID = “ZZZ”
p.prodDescription = “Last Gadget” p.listPrice = .99
p.available = True p.minStock = 1000 SaveRecord(BW, p)
BW.Close()
FS.Close()
End Sub
The code of the SaveRecord() subroutine is shown in Listing 13.19. It accepts as arguments the BinaryWriter object that represents the binary file to which the data will be written and a Product structure to be saved to the file.
Listing 13.19: The SaveRecord() Subroutine
Sub SaveRecord(ByVal writer As BinaryWriter, ByVal record As Product) writer.Write(record.ProdID)
writer.Write(record.prodDescription)
writer.Write(record.listPrice)
writer.Write(record.available)
writer.Write(record.minStock) End Sub
To read the records stored in the file, set up a BinaryReader associated with the Records.bin file and call the appropriate Read method for each field of the record. Since we don’t know in advance how many records are in the file, we set up a loop that keeps reading one record at a time, while the current position (property Position of the FileStream object) is less than the length of the file (property Length of the FileStream object). Listing 13.20 is the code behind the Read Records button.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |