|
|
 |
 |
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
Chapter 6 Text Boxes And Rich Text Boxes
- If you need an immediate solution to:
- Creating Multiline, Word-Wrap Text Boxes
- Aligning Text In Text Boxes
- Adding Scroll Bars To Text Boxes
- Making A Text Box Read-Only
- Accessing Text In A Text Box
- Selecting And Replacing Text In A Text Box
- Copying Or Getting Selected Text To Or From The Clipboard
- Creating A Password Control
- Controlling Input In A Text Box
- Adding An RTF Box To A Form
- Accessing Text In A Rich Text Box
- Selecting Text In Rich Text Boxes
- Using Bold, Italic, Underline, And Strikethru
- Indenting Text In Rich Text Boxes
- Setting Fonts And Font Sizes In Rich Text Boxes
- Using Bullets In Rich Text Boxes
- Aligning Text In A Rich Text Box
- Setting Text Color In RTF Boxes
- Moving The Insertion Point In RTF Boxes
- Adding Superscripts And Subscripts In Rich Text Boxes
- Setting The Mouse Pointer In Text Boxes And Rich Text Boxes
- Searching For (And Replacing) Text In RTF Boxes
- Saving RTF Files From Rich Text Boxes
- Reading RTF Files Into A Rich Text Box
- Printing From A Rich Text Box
In Depth
In this chapter, were going to start working with Visual Basic controlsin this case, text boxes and rich text boxes. Every Windows user is familiar with text boxes. Theyre exactly what their name implies: box-like controls in which you can enter text. Text boxes can be multiline, have scroll bars, be read-only, and have many other attributes, as well see in this chapter. Not every Windows user is familiar with rich text boxes, on the other hand. Rich text boxes (also known as RTF boxes) support not only plain text, but also Rich Text Format (RTF) text.
RTF text supports a variety of formats. For example, you can color text in a rich text box, underline it, bold it, or make it italic. You can select fonts and font sizes, as well as write the text out to disk or read it back in. RTF boxes can also hold a great amount of data, unlike standard text boxes, which are limited to 64K characters.
RTF text was designed to be a step beyond plain text, and because many word processors let you save text in that format, it can provide a link between different types of word processors. Using RTF boxes, you can also create your own simple word processors, and thats exactly what the Visual Basic Application Wizard does if you create an application with it. Youll find that the child windows in an Application Wizard program have a rich text box stretched across them, ready for the user to put to work.
How do you create text boxes and RTF boxes? As with other Visual Basic controls, you use the toolbox, as shown in Figure 6.1. In that figure, the Text Box tool is the second tool down on the right, and the RTF Box tool (which you add to a project with the Project|Components boxs Controls tab) appears at lower right.
Figure 6.1 The Text Box and RTF Box tools.
Use Of Text Boxes And RTF Boxes In Windows Programs
In Windows programs, text boxes and RTF boxes are used to handle text-based data, and not to let the user enter commands. When Windows first appeared, DOS-oriented programmers used to use text boxes to accept text-based commands from the user, but Microsoft considers that an abuse of the Windows user interface. The user is supposed to issue commands to a program with standard Windows controls like menu items, command buttons, radio buttons, toolbars, and so forth, not by typing command syntax into a text box. Text boxes and RTF boxes can certainly hold data that commands require for execution, but those controls are not usually intended to hold the commands themselves.
With all that in mind, then, lets start working with text boxes and RTF boxes. These are two of the most fundamental controls in Windows, and two of the most fun to work with. Well cover text boxes first in the Immediate Solutions and then turn to rich text boxes.
Immediate Solutions
Creating Multiline, Word-Wrap Text Boxes
Youve got a text box all set up for user feedback, and it can hold about 60 characters of text. Surely thats enough, you think. But when you start actually reading the users comments, you find that theyre all favorable, but truncated (I loved your program! In fact, let me say that I never saw a). Maybe its worthwhile to allow the user to enter more text.
You can do that by setting the text boxs MultiLine property to True, converting a text box into a multiline text box, complete with word wrap. The result appears in Figure 6.2. Now your programs users can type in line after line of text.
Figure 6.2 Creating a multiline text box.
Note that you can also add scroll bars to multiline text boxes. (See Adding Scroll Bars To Text Boxes later in this chapter.)
Aligning Text In Text Boxes
The Aesthetic Design Department has sent you a memo. Your new program meets its requirements for design standards, except for one thing: all the text boxes in your program are stacked one on top of the other, and the Aesthetic Design Department thinks it would be terrific if you display the text in those boxes as centered, not left-justified.
Well, you seem to remember that text boxes have an Alignment property, so you set it to Centered at design time in all the text boxes (there are three possibilities: 0 for left-justified, 1 for right-justified, and 2 for centered). You run your programand the text you enter ends up being left-justified. The Alignment property doesnt seem to work. Whats wrong?
You need to set the text boxes MultiLine property to True before text alignment will work; thats one of the quirks of text boxes. When you set the MultiLine property to True, everything works as it should, as you see in Figure 6.3.
Figure 6.3 Aligning text in a text box.
|