Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


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

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Setting Max And Min Font Sizes

The Testing Department is calling again. Now users are setting the font size in your program, SuperDuperTextPro, to 3 points—and then complaining they can’t read what they’ve typed. Can you limit the allowed font range?

Yes, you can, using the Common Dialog control’s Min and Max properties. When you want to make these properties active with a Font dialog box, you must first add the cdlCFLimitSize flag to the Common Dialog control’s Flags property. Then you’re free to restrict the possible range of font sizes.

Here’s an example. We set the Common Dialog’s CancelError property to True to catch Cancel button clicks, then set the Flags property of the Common Dialog control to display both screen fonts and printer fonts, and set the cdlCFLimitSize flag:

Private Sub Command1_Click()
    On Error GoTo Cancel
    CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize
...

Then we set the minimum and maximum font sizes we want to allow, measured in points:

Private Sub Command1_Click()
    On Error GoTo Cancel
    CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize
    CommonDialog1.Min = 12
    CommonDialog1.Max = 24
...

Finally, we show the Font dialog box, and then make use of the newly set font size:

Private Sub Command1_Click()
    On Error GoTo Cancel
    CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize
    CommonDialog1.Min = 12
    CommonDialog1.Max = 24
    CommonDialog1.ShowFont
    Text1.FontName = CommonDialog1.FontSize
Cancel:
End Sub

That’s all we need—the result of this code appears in Figure 11.11, where, as you can see, we’ve restricted the range of font sizes from 12 to 24 points in the Font dialog box.


Figure 11.11  Restricting font size range in a Font dialog box.


TIP:  Note that because the font size is entered in a combo box in a Font dialog box, the user can enter a value outside the allowed range in the text box part of the combo. If they do and click on OK, however, an error message box appears saying the font size must be in the Min to Max range.

Using The Print Dialog Box

The Testing Department is calling again. The Print button you’ve placed in your word processor, SuperDuperTextPro, is very nice, but it doesn’t let the user set the number of copies of a document they want to print. You can’t do that with a button, you explain. Right, they say—use a Print dialog box.

You show the Print dialog box with the Common Dialog control’s ShowPrinter method. If you know your document’s length, you can set the minimum and maximum pages to print in the Common Dialog control’s Min and Max properties; setting these properties enables the From and To page range text boxes in the Print dialog box (see “Setting The Minimum And Maximum Pages To Print” later in this chapter). You can also set the Common Dialog control’s Flags property to select various options here—see the next topic in this chapter.

This dialog box does not send data to the printer; instead, it lets the user specify how he wants data printed. Printing is up to you.

How do you print? If you’ve set the PrinterDefault property to True, you can use the Printer object to print data (the user can change the default printer from the Printer dialog box, setting a new default printer in the Windows registry or win.ini, but that new printer automatically becomes the one referred to by the Printer object). For example, you can print the picture in a picture box using the Printer object this way: Printer.PaintPicture Picture1.Picture, 0, 0. Otherwise, you must use Windows functions to print to the device represented by the hDC (a device context handle) property.

After the user clicks on OK, you can read these properties from the Common Dialog control to determine what printer options they’ve selected:

  Copies—The number of copies to print
  FromPage—The page to start printing
  ToPage—The page to stop printing
  hDC—The device context for the selected printer

Let’s see an example. In this case, we’ll use the Visual Basic PrintForm method to print a copy of the current form as many times as the user specifies. We start by setting the Common Dialog control’s CancelError property to True so we can catch Cancel button clicks as trappable errors:

Private Sub Command1_Click()
    On Error GoTo Cancel
...
Cancel:
End Sub

Then we set the PrinterDefault property to True and show the Print dialog box:

Private Sub Command1_Click()
    On Error GoTo Cancel
    CommonDialog1.PrinterDefault = True
    CommonDialog1.ShowPrinter
...
Cancel:
End Sub

All that’s left is to loop over the number of copies the user has requested (as returned in the Copies property) and call PrintForm each time:

Private Sub Command1_Click()
    Dim intLoopIndex As Integer

    On Error GoTo Cancel
    CommonDialog1.PrinterDefault = True
    CommonDialog1.ShowPrinter
    For intLoopIndex = 1 To CommonDialog1.Copies
        PrintForm
    Next intLoopIndex

Cancel:
End Sub

That’s it—when the user clicks Command1, the program displays the Print dialog box; the user can set the number of copies to print and when they click on OK, Visual Basic displays a dialog box with the text “Printing…” momentarily, and the print job starts.

Our Print dialog box example is a success—the code for this program is located in the printerdialog folder on this book’s accompanying CD-ROM.


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-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.