home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


Output-Only Text Boxes

Every part of a user interface provides some sort of feedback to the user. The shape, arrangement, border style, and color of objects on the screen give the user important clues to the objects’ use. For example, gray objects are not editable, but white objects are. Label controls have gray backgrounds to indicate that the user is not expected to enter data in them. Text boxes have white backgrounds to indicate that user input is expected.

Applications should provide similar clues where possible. If an application changes the background colors of objects, it should do so consistently. If the application’s text boxes and labels are randomly assigned all the colors of the rainbow, users will have a hard time deciding where they should enter data and where they should not.

ExpenseReporter provides additional clues for output-only text boxes. The expense total field and the fields that display the amounts due to the company and employee have a special meaning. Unlike most labels, the values of these fields change frequently. Unlike text boxes, the user cannot enter data into these fields.

ExpenseReporter sets these fields apart by making them output-only text boxes. These are normal text boxes with the Locked property set to true. Setting Locked to true prevents the user from editing the fields. The background colors of the text boxes are set to gray so it is obvious to the user that the fields are not editable.

A side benefit to using text boxes for these fields rather than label controls is that this allows the user to select text in the controls. The text can then be copied to the clipboard by pressing Ctrl-C so that it can later be pasted into other controls. For these small fields this benefit is minor. Cutting and pasting the results of a long report, however, could be quite useful.

Formatting Text

Right-justifying dollar amounts makes them easier to read and understand. Figure 2.1 shows two columns of numbers. The values on the left do not have the same number of digits after the decimal point and are aligned on the left. The numbers on the right all have two digits after the decimal point and are right-justified. Notice how much easier it is to find the largest dollar amounts in the list on the right.

Visual Basic’s text box controls have an Alignment property that can be set to vbRightJustify to make a field right-justified. If several of these fields are aligned on their right edges, their text will be aligned in a column.

Unfortunately Visual Basic ignores the Alignment property unless the control’s MultiLine property is also set to true. To make a single line text box that is right-justified, an application can set the control’s Alignment property to vbRightJustify and its MultiLine property to true. Then any carriage return characters the user enters can be discarded in the control’s KeyPress event handler, thereby ensuring that the input remains visible on a single line.

The application must also consider characters added to the field by cutting and pasting. The application cannot allow the user to copy text containing a carriage return to the clipboard and then paste it into the right-justified field. ExpenseReporter’s money fields use this technique to remain right-justified. The money validation routine ValidMoney explicitly disallows carriage return keys.

Printing

Printing is one of the most complex issues in application programming. Different types of printers provide hundreds of features. Writing routines that take full advantage of a printer’s capabilities can be as challenging as building the rest of the application.

Visual Basic’s PrintForm method allows an application to provide a simple printing function easily. When the application invokes a form’s PrintForm method, Visual Basic sends a bitmap image of the form to the current printer.


FIGURE 2.1  Dollar amounts that are right-justified are easier to read.

ExpenseReporter’s File menu contains a PrintForm command for experimentation purposes, but that command’s Visible property is set to false. It can be made visible by setting its Visible property to true using the Menu Editor.

The PrintForm method has the advantage of requiring almost no programming, but it has some big disadvantages as well.

PrintForm creates a bitmap image of the form using the screen’s resolution. A typical screen has a fairly low resolution—less than 100 pixels per inch. Most printers have a much higher resolution—typically 300 or more dots per inch. When a low-resolution image is printed on a high-resolution printer, the result is somewhat less than satisfying. Screen pixels appear as square blocks on the printout, making lines appear abnormally jagged. Text is often ugly and hard to read. Figure 2.2 shows text printed at low and high resolutions.

The PrintForm method also attempts to reproduce form colors. In particular, it tries to use the printer’s capabilities to make a typical form’s background appear gray. Unfortunately, this tends to make the form look spotted. It can also make text, particularly label controls, nearly impossible to read.

A potentially more important problem occurs for forms that have controls in scrolling regions. Because PrintForm displays an exact image of whatever is visible on the form, controls that are scrolled out of the visible area are not printed.

The PrintForm method has its problems, but it is extremely easy to use. For that reason it is very convenient during the early stages of application development. When the project is closer to completion and control arrangements are fairly stable, PrintForm can be replaced with the more advanced printing techniques described in the following sections.

The Printer Object

A Visual Basic application prints high-resolution output using the Printer object. This object represents the system’s default printer. The Printer object supports many of the same properties and methods supported by forms and picture boxes. For example, all three of these objects have a Line method that draws lines and boxes. An application could use the following code to draw a one-inch square box on the Printer object, two inches from the upper left corner. Note that the printer measures distances in twips and that there are 1440 twips in an inch.


FIGURE 2.2  Text printed at low and high resolutions.

Printer.Line (2 * 1440, 2 * 1440)-Step(1440, 1440), , B

Printers, forms, and picture boxes also have Circle, PaintPicture, Print, PSet, TextHeight, and Text-Width methods. Using these methods an application can create high-resolution output for the printer.

The Printer object has a few additional methods that forms and picture boxes do not:

•  NewPage tells the printer that the program is finished sending output to be drawn on the current page and that the Printer object should begin a new one.
•  EndDoc tells Visual Basic that the program is done creating the document and that it should be sent to the physical printer for printing.
•  KillDoc cancels the current print job. An application should end each print session with either EndDoc or KillDoc.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.