|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Adding Scroll Bars To Text BoxesNow that youre 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 programs 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:
Note that in order for the scroll bars to actually appear, the text boxs 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.
Making A Text Box Read-OnlyThere 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 doesnt enter text there by mistake. Heres 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 programs 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 cant tell if a text box is locked until they try to enter text in it!)
Disabling A Text Box You can also disable a text box by setting its Enabled property to False. However, although this means the user cant 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 labels Caption property. Accessing Text In A Text BoxJava, C++, Visual Basica 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.
Selecting And Replacing Text In A Text BoxTo work with part of the text in a text box, you select the text you want using three properties:
For example, heres 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
Thats 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 ClipboardAfter entering their new novels into your program, users were surprised that they couldnt 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:
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. Heres 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
|
|
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. |