Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Visual Basic 2005 (2006)

.pdf
Скачиваний:
219
Добавлен:
17.08.2013
Размер:
14.97 Mб
Скачать

Chapter 7

Property

Description

 

 

MaxSize

Indicates the maximum size (in points) a user can select.

MinSize

Indicates the minimum size (in points) a user can select.

ShowApply

Indicates whether the dialog box contains an Apply button.

ShowColor

Indicates whether the dialog box displays the color choice.

ShowEffects

Indicates whether the dialog box contains controls that allow the

 

user to specify strikethrough, underline, and text color options.

ShowHelp

Indicates whether the dialog box displays a Help button.

 

 

The Methods of FontDialog

You will only be using one method (ShowDialog) of FontDialog in the forthcoming Try It Out. Other methods available include Reset, which allows you to reset all the properties to their default values.

Using the FontDialog Control

You can display the FontDialog control without setting any properties:

FontDialog1.ShowDialog()

The dialog box would then look like Figure 7-10.

Figure 7-10

226

Displaying Dialog Boxes

Notice that the Font dialog box contains an Effects section that enables you to check the options for Strikeout and Underline. However, color selection of the font is not provided by default. If you want this, you must set the ShowColor property before calling the ShowDialog method on the dialog box:

FontDialog1.ShowColor = True

FontDialog1.ShowDialog()

The ShowDialog method of this dialog box, like all of the ones that you have examined thus far, returns a DialogResult. This will be either DialogResult.OK or DialogResult.Cancel.

Once the dialog box returns, you can query for the Font and Color properties to see what font and color the user has chosen. You can then apply these properties to a control on your form or store them to a variable for later use.

Now that you know what the Font dialog looks like and how to call it, you can use it in a Try It Out. You need to use the program from the last two Try It Outs to open a file, and have the contents of the file read into the text box on the form. You then use the FontDialog control to display the Font dialog box, which allows you to select a font. Then you change the font in the text box to the font that you have chosen.

Try It Out

Working with FontDialog

1.Open the Dialogs project again.

2.On the form add another button from the Toolbox and set its properties according to the values shown in this list:

Set Name to btnFont.

Set Anchor to Top, Right.

Set Location to 367, 68.

Set Text to Font.

3.You now need to add the FontDialog control to your project, so locate this control in the Toolbox and drag and drop it onto the form in the workspace below the form or on the form itself; the control will be automatically placed in the workspace below the form. Accept all default properties for this control.

4.You want to add code to the Click event of the Font button, so double-click it and add the following highlighted code:

Private Sub btnFont_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnFont.Click

‘Set the FontDialog control properties FontDialog1.ShowColor = True

‘Show the Font dialog

If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then ‘If the OK button was clicked set the font

‘in the text box on the form

227

Chapter 7

txtFile.Font = FontDialog1.Font

‘Set the color of the font in the text box on the form

txtFile.ForeColor = FontDialog1.Color End If

End Sub

5.Test your code by clicking the Start button on the toolbar. Once your form has been displayed, click the Font button to display the Font dialog box as shown in Figure 7-11. Choose a new font and color and then click OK.

6.Now add some text in the text box on your form. The text will appear with the new font and color that you have chosen.

Figure 7-11

7.This same font and color will also be applied to the text that is loaded from a file. To demonstrate this, click the Open button on the form and open a text file. The text from the file is displayed in the same font and color that you chose in the Font dialog.

How It Works

You know that the Font dialog box does not show a Color box by default, so you begin by setting the ShowColor property of the FontDialog control to True so that the Color box is displayed:

‘Set the FontDialog control properties FontDialog1.ShowColor = True

Next, you actually show the Font dialog box. Remember the DialogResult returns a value of OK or Cancel, so that you can compare the return value from the FontDialog control to Windows.Forms. DialogResult.OK. If the button that the user clicked was OK, you execute the code within the If . . .

End If statement:

‘Show the Font dialog

If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

228

Displaying Dialog Boxes

‘If the OK button was clicked, set the font ‘in the text box on the form

txtFile.Font = FontDialog1.Font

‘Set the color of the font in the text box on the form txtFile.ForeColor = FontDialog1.Color

End If

You set the Font property of the text box ( txtFile) equal to the Font property of the FontDialog control. This is the font that the user has chosen. Then you set the ForeColor property of the text box equal to the Color property of the FontDialog control, as this will be the color that the user has chosen. After these properties have been changed for the text box, the existing text in the text box is automatically updated to reflect the new font and color. If the text box does not contain any text, any new text that is typed or loaded into the text box will be of the new font and color.

The ColorDialog Control

Sometimes you may need to allow the user to customize the colors on their form. This may be the color of the form itself, a control, or of text in a text box. Visual Basic 2005 provides the ColorDialog control for all such requirements. Once again, the ColorDialog control can also be used as a class — declared in code without dragging a control onto the Form Designer.

The ColorDialog control, shown in Figure 7-12, allows the user to choose from 48 basic colors.

Figure 7-12

Notice that the users can also define their own custom colors, adding more flexibility to your applications. When the users click the Define Custom Colors button in the Color dialog box, they can adjust the color to suit their needs (see Figure 7-13).

Having this opportunity for customization and flexibility in your applications gives them a more professional appearance, plus your users are happy because they are allowed to customize the application to meet their own personal tastes.

229

Chapter 7

Figure 7-13

The Properties of ColorDialog

Before you dive into some code, take a look at some of the available properties for the ColorDialog control, shown in Table 7-9.

Property

Description

 

 

AllowFullOpen

Indicates whether the user can use the dialog box to define custom colors.

AnyColor

Indicates whether the dialog box displays all available colors in the set of

 

basic colors.

Color

Indicates the color selected by the user.

CustomColors

Indicates the set of custom colors shown in the dialog box.

FullOpen

Indicates whether the controls used to create custom colors are visible

 

when the dialog box is opened.

ShowHelp

Indicates whether a Help button appears in the dialog box.

SolidColorOnly

Indicates whether the dialog box will restrict users to selecting solid colors

 

only.

 

 

There aren’t many properties that you need to worry about for this dialog box, which makes it even simpler to use than the other dialog boxes that you have examined so far.

As with the other dialog box controls, ColorDialog contains a ShowDialog method. You have already seen this method in the previous examples, and since it is the same, it does not need to be discussed again.

230

Displaying Dialog Boxes

Using the ColorDialog Control

All you need to do to display the Color Dialog box is to execute its ShowDialog method:

ColorDialog1.ShowDialog()

The ColorDialog control will return a DialogResult of OK or Cancel. Hence, you can use the previous statement in an If . . . End If statement and test for a DialogResult of OK, as you have done in the previous examples that you have coded.

To retrieve the color that the user has chosen, you simply retrieve the value set in the Color property and assign it to a variable or any property of a control that supports colors, such as the ForeColor property of a text box:

txtFile.ForeColor = ColorDialog1.Color

In the next Try It Out, you continue using the same project and make the ColorDialog control display the Color dialog box. Then, if the dialog box returns a DialogResult of OK, you change the background color of the form.

Try It Out

Working with the ColorDialog Control

1.Switch to the Forms Designer in the Dialogs project.

2.On the form, add another Button control from the Toolbox and set its properties according to the values shown:

Set Name to btnColor.

Set Anchor to Top, Right.

Set Location to 367, 98.

Set Text to Color.

3.Next, add a ColorDialog control to your project from the Toolbox. It will be added to the workspace below the form, and you will accept all default properties for this control.

4.Double-click the Color button to bring up its Click event handler and add the following highlighted code:

Private Sub btnColor_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnColor.Click

‘Show the Color dialog

If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then ‘Set the BackColor property of the form

Me.BackColor = ColorDialog1.Color End If

End Sub

5.That’s all the code you need to add. To test your changes to this project, click the Start button.

231

Chapter 7

6.Once the form is displayed, click the Color button to display the Color dialog box. Choose any color that you want, or create a custom color by clicking the Define Custom Colors button. Once you have chosen a color, click the OK button in the Color dialog box.

7.The background color of the form will be set to the color that you chose and the background color of the buttons will inherit the background color of the form.

8.As with the Font dialog box, you do not have to set the Color property of the ColorDialog control before displaying the Color dialog box again. It automatically remembers the color chosen, and this will be the color that is selected when the dialog box is displayed again. To test this, click the Color button again, and the color that you chose will be selected.

How It Works

This time you did not need to set any properties of the ColorDialog control, so you jumped right in and displayed it in an If . . . End If statement to check the DialogResult returned by the ShowDialog method of this dialog box:

‘Show the Color dialog

If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

Within the If . . . End If statement, you added the code necessary to change the BackColor property of the form. Once the BackColor property was changed, each Button control on the form inherited the background color of the form; there was no code that you needed to write for this. The reason behind this is that the Button class is part of the System.Windows.Forms.Control namespace; thus, it automatically inherits the background color of the form:

‘Set the BackColor property of the form Me.BackColor = ColorDialog1.Color

The PrintDialog Control

Any application worth its salt will incorporate some kind of printing capabilities, whether it is basic printing or more sophisticated printing, such as allowing a user to print only selected text or a range of pages. In this next section of the chapter you explore basic printing. You take a look at several classes that help you to print text from a file.

Visual Basic 2005 provides the PrintDialog control. It does not actually do any printing but enables you to select the printer that you want to use and set the printer properties such as page orientation and print quality. It also enables you to specify the print range. You will not be using these features in this next example, but it is worth noting that this functionality is available in the PrintDialog control as shown in Figure 7-14.

Like the previous dialog boxes that you have examined, the Print dialog box provides OK and Cancel buttons; thus, its ShowDialog method returns a DialogResult of OK or Cancel. You can then use this result in an If . . . End If statement and test for the DialogResult.

232

Displaying Dialog Boxes

Figure 7-14

The Properties of PrintDialog

Take a quick look at some of the properties provided in PrintDialog shown in the following table.

Just like the other dialog boxes, PrintDialog exposes a ShowDialog method.

Property

Description

 

 

AllowPrintToFile

Indicates whether the Print to file check box is enabled.

AllowSelection

Indicates whether the Selection radio button is enabled.

AllowSomePages

Indicates whether the Pages radio button is enabled.

Document

Indicates the Print Document used to obtain the printer settings.

PrinterSettings

Indicates the printer settings that the dialog box will be modifying.

PrintToFile

Indicates whether the Print to file check box is checked.

ShowHelp

Indicates whether the Help button is displayed.

ShowNetwork

Indicates whether the Network button is displayed.

233

Chapter 7

Using the PrintDialog Control

The only method that you will be using is the ShowDialog method, which will display the Print dialog box shown in Figure 7-14. As mentioned earlier, the PrintDialog control merely displays the Print dialog box; it does not actually do any printing. The following code fragment shows how you display the Print dialog box:

PrintDialog1.ShowDialog()

The PrintDocument Class

Before you can call the ShowDialog method of the PrintDialog control, you have to set the Document property of the PrintDialog class. This property accepts a PrintDocument class, which is used to obtain the printer settings and can send output to the printer. This class requires the System.Drawing. Printing namespace, so you must include this namespace before attempting to define an object that uses the PrintDocument class.

The Properties of the PrintDocument Class

Before you continue, take a look at some of the important properties of the PrintDocument class, listed in the following table.

Property

Description

 

 

DefaultPageSettings

Indicates the default page settings for the document.

DocumentName

Indicates the document name that is displayed while printing the

 

document. This is also the name that appears in the Print Status dia-

 

log box and printer queue.

PrintController

Indicates the print controller that guides the printing process.

PrinterSettings

Indicates the printer that prints the document.

Printing a Document

The Print method of the PrintDocument class prints a document to the printer specified in the PrinterSettings property. When you call the Print method of the PrintDocument class, the PrintPage event is raised for each page as it prints. Therefore, you would need to create a procedure for that event and add an event handler for it. The procedure that you would create for the PrintPage event does the actual reading of your text file using the StreamReader object that you define.

Printing using the PrintDocument class requires a lot of coding and knowledge of how actual printing works. Fortunately, the .NET Framework provides the My.Computer.Printers namespace, which simplifies your job as a developer.

This namespace wraps up all the complexities of printing and provides you with methods and properties that allow you to print a text document with ease and just a few lines of code. You can use the DefaultPrinter method of this namespace to print to the default printer, or you can use the Item

234

Displaying Dialog Boxes

property to specify the printer that you want to print to. Using either one, you can print a text document with as little as two lines of code, as shown in this code snippet:

With My.Computer.Printers.DefaultPrinter

.WriteLine(txtFile.Text)

.Print() End With

Now that you know a little bit about how printing works, look at how all this fits together in a Try It Out.

Try It Out

Working with the PrintDialog Control

1.Open the Dialogs project.

2.On the form, add another button from the Toolbox and set its properties according to the values shown:

Set Name to btnPrint.

Set Anchor to Top, Right.

Set Location to 367, 128.

Set Text to Print.

3.Now add a PrintDialog control to the project, dragging and dropping it from the Toolbox onto the form. It will be added to the workspace below the form, and you will accept all default properties for this control.

4.Now switch to the Code Editor so that you can add the required namespaces for printing a file. Add these namespaces to the top of your class:

Imports System.IO

Imports System.Drawing.Printing

Public Class Dialogs

5.Now add the following variable declarations to the top of your class:

‘Declare variable

Private strFileName As String

Private objStreamToPrint As StreamReader

Private objPrintFont As Font

6.Select btnPrint in the Class Name combo box and the Click event in the Method Name combo box. Add the following highlighted code to the btnPrint_Click event procedure:

Private Sub btnPrint_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles btnPrint.Click

‘Declare an object for the PrintDocument class

Dim objPrintDocument As PrintDocument = New PrintDocument()

235