Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

Chapter 7

More Windows Controls

In this chapter, we’ll continue our discussion of the basic Windows controls with the controls that implement the common dialog boxes and the RichTextBox control.

The Toolbox contains a few more controls, like ToolBar and DateTimePicker, that I won’t discuss in this book. You won’t often need them in your applications, and once you have learned to build user interfaces with the basic Windows controls, you’ll have no problem using the less common ones. The controls I’m not discussing in this book are less elaborate and have a relatively small number of properties and methods.

The .NET Framework provides a set of controls for displaying common dialogs such as Open or Color. Each of these controls encapsulates a large amount of functionality that would take a lot of code to duplicate (in any language). The common dialog controls are an essential part of a Windows application, because they enable you to design user interfaces with the look and feel of a Windows application.

Besides the common dialog boxes, we’ll also explore the RichTextBox control, which is an advanced version of the TextBox. RichTextBox provides all the functionality you’ll need to build a word processor—WordPad is actually built around the RichTextBox control. It’s the only control that can display formatted text, so if your application requires this feature, RichTextBox is your only option. It allows you to format text by mixing any number of fonts and attributes. You can also embed other objects in the document displayed on a RichTextBox, such as images. Sure, the RichTextBox control is nothing like a modern word processor, but it’s a great tool for editing formatted text at runtime.

The Common Dialog Controls

A rather tedious, but quite common, task in nearly every application is to prompt the user for filenames, font names and sizes, or colors to be used by the application. Designing your own dialog boxes for these purposes would be a hassle, not to mention that your applications wouldn’t have the same look and feel of all Windows applications. In fact, all Windows applications use some standard dialog boxes for common operations. Figure 7.1 shows a couple of examples. These dialog boxes are built into the Framework system, and any application can use them.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

290 Chapter 7 MORE WINDOWS CONTROLS

Figure 7.1

The Open and Font common dialog boxes

If you ever want to display an Open or Font dialog box, don’t design it—it already exists. To use it, just place the appropriate common dialog control on your form and activate it from within your code by calling the ShowDialog method.

VB6 VB.NET

In previous versions of VB, there was a single control on the Toolbox for all common dialog controls. VB.NET has a separate control for each common dialog, and there are four such controls on the Toolbox (excluding the ones that apply to printing). The new common dialog controls are the FontDialog, ColorDialog, OpenFileDialog, and SaveFileDialog. The new controls expose a large number of properties that are specific to each dialog box and are a little easier to program than the previous single control. The dialog controls for printing are discussed in detail in Chapter 15.

The common dialog controls are invisible at runtime, and they’re not placed on your forms. You simply add them to the project by double-clicking their icons on the Toolbox. When a common dialog control is added to a project, a new icon appears in the components tray of the form, just below the Form Designer. The following common dialog controls are available on the Toolbox.

OpenFileDialog Lets users select a file to open. It also allows the selection of multiple files, for applications that must process many files at once (change the format of the selected files, for example).

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

THE COMMON DIALOG CONTROLS 291

SaveFileDialog Lets users select or specify a filename in which the current document will be saved.

ColorDialog Lets users select a color from a list of predefined colors, or specify custom colors.

FontDialog Lets users select a typeface and style to be applied to the current text selection. The Font common dialog has an Apply button, which you can intercept from within your code and use to apply the currently selected font to the text without closing the common dialog.

PrintDialog Lets users select and set up a printer (the page’s orientation, the document pages to be printed, and so on).

There are two more common dialog controls, the PrintPreviewDialog and the PageSetupDialog controls. These controls will be discussed in detail in Chapter 15, in the context of VB’s printing capabilities. The PrintDialog control is discussed here because it doesn’t require any printing code. This dialog box simply sets the basic properties of the printout. These properties must be taken into consideration by the printing code of the application, as you will see in Chapter 15.

Using the Common Dialog Controls

To display a common dialog from within your application, you must first add an instance of the appropriate control to your project. Then, you must set basic properties of the control through the Properties window. Most applications set the control’s properties from within the code, because common dialogs interact closely with the application. When you call the Color common dialog, for example, you should preselect a color from within your application and make it the default selection on the control. If you open the Color dialog box to prompt the user for the color of the text on a control, the default selection should be the current setting of the control’s ForeColor property. Likewise, the Save dialog box must suggest a filename when it first pops up, and you must specify the appropriate filename (or, at least, the file’s extension) from within your application’s code.

To display a common dialog box from within your code, you simply call the control’s ShowDialog method, which is common for all controls. As soon as you call the ShowDialog method, the corresponding dialog box appears on-screen and the execution of the program is suspended until the box is closed. Using the Open and Save dialog boxes, the user can traverse the entire structure of their drives and locate the desired filename. When the user clicks the Open or Save button, the dialog box closes and the program’s execution resumes. The code should read the name of the file selected by the user (FileName property) and use it to open the file or to store the current document there.

Here is the sequence of statements used to invoke the Open common dialog and retrieve the selected filename:

If OpenFileDialog1.ShowDialog = DialogResult.OK Then

fileName = OpenFileDialog1.FileName

End If

The common dialogs are nothing more than dialog boxes, and they return a value indicating how they were closed. You should read this value from within your code and ignore the settings of the dialog box if it was canceled.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

292 Chapter 7 MORE WINDOWS CONTROLS

The variable fileName is the full pathname of the file selected by the user. You can also set the FileName property to a filename, which will be displayed when the Open dialog box is first opened. This allows the user to click the Open button to open the preselected file or choose another file.

OpenFileDialog1.FileName = “C:\Documents\Doc1.doc”

If OpenFileDialog1.ShowDialog = DialogResult.OK Then

fileName = OpenFileDialog1.FileName

End If

Similarly, you can invoke the Color dialog box and read the value of the selected color with the following statements:

If ColorDialog1.ShowDialog = DialogResult.OK Then

selColor = ColorDialog1.Color

End If

The ShowDialog method is common to all controls. The Title property is also common to all controls. This property returns or sets the string displayed in the title bar of the dialog box. The default title is the name of the dialog box (e.g., “Color,” Font,” and so on), but you can adjust it from within your code.

The Color Dialog Box

The Color dialog box, shown in Figure 7.2, is one of the simplest dialog boxes. It has a single property, Color, which returns the color selected by the user or sets the initially selected color when the user opens the dialog box. Before opening the Color common dialog with the ShowDialog method, you can set various properties, which are described next.

Figure 7.2

The Color dialog box

The following statements set the selected color of the Color common dialog control, display the control, and then use the color selected on the control to fill the form. First, place a ColorDialog control on the form, and then insert the following statements in a button’s Click event handler:

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

ByVal e As System.EventArgs) Handles Button1.Click

ColorDialog1.Color = Me.BackColor

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

THE COMMON DIALOG CONTROLS 293

ColorDialog1.AllowFullOpen = True

If ColorDialog1.ShowDialog = DialogResult.OK Then

Me.BackColor = ColorDialog1.Color

End If

End Sub

AllowFullOpen

Set this property to True if you want users to be able to open up the dialog box and define their own custom colors. The AllowFullOpen property doesn’t open the custom section of the common dialog. It simply enables the Define Custom Colors button on the dialog box. Otherwise, this button is disabled. If you want to fully open the Color dialog box (like the one shown in Figure 7.2) when it first pops up, set the AllowFullOpen property to True. The Define Custom Colors button on the dialog box of Figure 7.2 is disabled because the dialog box is already fully opened.

AnyColor

This property is a Boolean value that determines whether the dialog box displays all available colors in the set of basic colors.

Color

This property is a Color value, and you can set it to any valid color. If you set this property before opening the Color dialog box, the selected color will appear on the control as the preselected color. On return, it’s the color selected by the user on the dialog box.

ColorDialog1.Color = Color.Azure

If ColorDialog1.ShowDialog = DialogResult.OK Then

Me.BackColor = ColorDialog1.Color

End If

CustomColors

This property indicates the set of custom colors that will be shown in the common dialog. The Color dialog box has a section called Custom Colors, where you can display 16 additional custom colors. The CustomColors property is an array of integers that represent colors. To display three custom colors in the lower section of the Color dialog box, use a statement like the following:

Dim colors() As Integer = {222663, 35453, 7888}

ColorDialog1.CustomColors = colors

You’d expect that the CustomColors property would be an array of Color values, but it’s not. You can’t create the array CustomColors with a statement like:

Dim colors() As Color = {Color.Azure, Color.Navy, Color.Teal}

Since it’s awkward to work with numeric values, you should convert color values to integer values with a statement like the following:

Color.Navy.ToARGB

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com