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


Adding Scroll Bars To Text Boxes

Now that you’re using multiline text boxes, it would be even better if you could add scroll bars to let the user enter even more text. If your program’s users are going to be entering a lot of text into text boxes, you can avoid the need for huge text boxes by adding scroll bars.

Using the ScrollBars property, there are four ways to add scroll bars to a text box. Here are the settings you use for the ScrollBars property, and the type of scroll bars each setting displays:

  0—None
  1—Horizontal
  2—Vertical
  3—Both

Note that in order for the scroll bars to actually appear, the text box’s MultiLine property must be True. After you install scroll bars in a text box, the result appears as in Figure 6.4. Now the user can enter much more text simply by scrolling appropriately.


Figure 6.4  Using scroll bars in a text box.


TIP:  Although text boxes can hold up to 64K characters, that may be too much for you to conveniently handle, and you may want to limit the maximum number of characters a text box can hold. You do that by setting the text box’s MaxLength property to the maximum number of characters you want the user to be able to enter (the default value for MaxLength is 0, which actually means 64K characters).

Making A Text Box Read-Only

There are times when you want to make text boxes read-only. For example, you might have written a calculator program in which you let the user enter operands in text boxes and display the result in another text box. The result text box should be read-only so that the user doesn’t enter text there by mistake. Here’s how you do that.

Locking A Text Box

You use the Locked property to make a text box read-only. Setting this property to True means that the user cannot enter text into the text box except under your program’s control, like this:

Private Sub Command1_Click()
    Text1.Text = "This box is locked."
End Sub

An example of a locked text box appears in Figure 6.5 (note that users can’t tell if a text box is locked until they try to enter text in it!)


Figure 6.5  A locked text box.

Disabling A Text Box

You can also disable a text box by setting its Enabled property to False. However, although this means the user can’t enter text into the text box, it also means the text in the box appears grayed. Disabling is better done to indicate that the control is inaccessible.

Using Labels Instead Of Text Boxes

Another alternative to using read-only text boxes is to display read-only text in label controls. (Label controls can hold as much text as a text box.) You can change the text in a label control from code using the label’s Caption property.

Accessing Text In A Text Box

Java, C++, Visual Basic—a programmer has to switch between a lot of languages these days. So how do you set the text in a text box again? Is there a SetText() method?

No, you use the Text property like this:

Private Sub Command1_Click()
Text1.Text = "Hello from Visual Basic"
End Sub

When the user clicks the command button Command1, the text “Hello from Visual Basic” appears in the text box, as shown in Figure 6.6.


Figure 6.6  Setting a text box’s text.

Selecting And Replacing Text In A Text Box

To work with part of the text in a text box, you select the text you want using three properties:

  SelLength—Returns or sets the number of characters selected.
  SelStart—Returns or sets the starting point of selected text. If no text is selected, SelStart indicates the position of the insertion point.
  SelText—Returns or sets the string containing the currently selected text. If no characters are selected, SelText consists of a zero-length string (“”).

For example, here’s how we select all the text in a text box and replace it with “Welcome to Visual Basic” (which we could have done just as easily by assigning that string to the Text property, of course). Note the use of Len() to get the length of the text currently in the text box:

Private Sub Command1_Click()
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1.Text)
    Text1.SelText = "Welcome to Visual Basic"
End Sub

That’s how it works when you want to select some text: you specify the beginning of the selected text in SelStart, the end in SelLength, and refer to the text with the SelText property.

Note that text selected under program control this way does not appear highlighted in the text box.

The HideSelection Property

While on the topic of text selection, we might note the HideSelection property, which, when True, turns off text-selection highlighting when your program loses the focus.

Copying Or Getting Selected Text To Or From The Clipboard

After entering their new novels into your program, users were surprised that they couldn’t copy them to the Clipboard and paste them into other applications. How can you support the Clipboard with text in a text box?

You can copy selected text to the Clipboard using SetText:

Clipboard.SetText text, [format]

Here, text is the text you want to place into the Clipboard, and format has these possible values:

  vbCFLink—&HBF00; DDE conversation information
  vbCFRTF—&HBF01; Rich Text Format
  vbCFText—1 (the default); Text

You can get text from the clipboard using the GetText() function this way

Clipboard.GetText([format])

where format can be taken from the earlier list of possible format types.

Here’s an example to make this clearer; in this case, we place all the text in text box Text1 into the clipboard:

Private Sub Command1_Click()
    Clipboard.SetText Text1.Text
...
End Sub

Then we read the text back and display it in a new text box, Text2:

Private Sub Command1_Click()
    Clipboard.SetText Text1.Text
    Text2.Text = Clipboard.GetText
End Sub


TIP:  Text boxes already allow the user to use these shortcuts to work with the Clipboard: Ctrl+C to copy selected text, Ctrl+V to paste text from the clipboard, and Ctrl+X to cut selected text.


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.