Click Here!
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


Setting The Mouse Pointer In Text Boxes And Rich Text Boxes

You can set the mouse pointer when it travels over a text box or rich text box. Just set the Mousepointer property to one of the values in Table 6.1.

Table 6.1 Mouse pointer options.
Constant Value Description
rtfDefault 0 (Default) Shape determined by the object
rtfArrow 1 Arrow
rtfCross 2 Cross (cross-hair pointer)
rtfIbeam 3 I beam
rtfIcon 4 Icon (small square within a square)
rtfSize 5 Size (four-pointed arrow pointing north, south, east, and west)
rtfSizeNESW 6 Size NE SW (double arrow pointing northeast and southwest)
rtfSizeNS 7 Size N S (double arrow pointing north and south)
rtfSizeNWSE 8 Size NW, SE
rtfSizeEW 9 Size E W (double arrow pointing east and west)
rtfUpArrow 10 Up arrow
rtfHourglass 11 Hourglass (wait)
rtfNoDrop 12 No drop
rtfArrowHourglass 13 Arrow and hourglass
rtfArrowQuestion 14 Arrow and question mark
rtfSizeAll 15 Size all
rtfCustom 99 Custom icon specified by the MouseIcon property

Searching For (And Replacing) Text In RTF Boxes

The users of your popular new word processor, SuperDuperTextPro, are still not satisfied. They find it inconvenient to search through 300-page documents for a particular word. Can you add search capability to your program? Better yet, they ask, how about search and replace?

Any word processor of any value will let the user search for text, and rich text boxes do that with the Find() method. For example, if we placed this text in a rich text box:

Private Sub Form_Load()
    RichTextBox1.Text = "Here is some text."
End Sub

Next, we could search for the word “some” this way with Find():

Private Sub Command1_Click()
    RichTextBox1.Find ("some")
...
End Sub

After you find an item, it becomes the new selection. So, if we wanted to replace the word “some” with, say, “the”, we could do that this way:

Private Sub Command1_Click()
    RichTextBox1.Find ("some")
    RichTextBox1.SelRTF = "the"
End Sub

In this way, we search for the word “some” in the text and replace it with “the”, as shown in Figure 6.16.


Figure 6.16  Searching for and replacing text.

Saving RTF Files From Rich Text Boxes

You’ve gotten feedback from a user of your word processor, SuperDuperTextPro, and it seems she’s written a 600-page novel with the program and now finds there’s no way to save it to disk. Can you help? She will keep her computer on until she hears from you.

You use the SaveFile() method to save the text in a rich text box to disk, and doing that is really easy—you just use SaveFile() this way:

RichTextBox.SaveFile(pathname, [filetype])

You can save text as plain or RTF text; the settings for filetype are as follows:

  rtfRTF—0(the default); the RichTextBox control saves its contents as an RTF file.
  rtfText—1; the RichTextBox control saves its contents as a text file.

Here’s an example where we display some text in a rich text box:

Private Sub Form_Load()
    RichTextBox1.Text = "This is the text in the file."
End Sub

Next, we save that text to a file this way:

Private Sub Command1_Click()
    RichTextBox1.SaveFile ("c:\data.txt")
End Sub

And that’s all it takes. Now we’ve written RTF to a file.


TIP:  Many word processors, like Microsoft Word, support RTF files, so you can now write text formatted files that such word processors can read in and use.

Reading RTF Files Into A Rich Text Box

You can write files to disk from a rich text box with SaveFile(); how can you read files back in? You use LoadFile().

Like SaveFile(), LoadFile() is very easy to use:

RichTextBox.LoadFile pathname, [filetype]

And you can load in plain text or RTF text files; the settings for filetype are as follows:

  rtfRTF—0(The default); the RichTextBox control saves its contents as an RTF file.
  rtfText—1; the RichTextBox control saves its contents as a text file.

Here’s an example where we load in the file we wrote in the last topic on saving files, data.txt:

Private Sub Command1_Click()
    RichTextBox1.LoadFile "c:\data.txt"
End Sub

That’s all there is to it—it’s that easy to load in files.

Printing From A Rich Text Box

You can print from a rich text box using the SelPrint() method and the Visual Basic Printer object. The only thing to remember here is that you should first initialize the printer by printing a string of zero length or similar operation.

Here’s how we print the last two words in the text “Printing this text…”; first, we display that text in the rich text box:

Private Sub Form_Load()
    RichTextBox1.Text = "Printing this text..."
End Sub

Next, we select the last two words:

Private Sub Command1_Click()
    RichTextBox1.Find ("this text…")
    RichTextBox1.SelLength = Len("this text…")
...

Finally, we print them. Note that we have to pass the handle of the device context with which we want to print to SelPrint(), and here, that’s the Printer object’s device context, Printer.hDC:

Private Sub Command1_Click()
    RichTextBox1.Find ("this text...")
    RichTextBox1.SelLength = Len("this text...")
    Printer.NewPage
    RichTextBox1.SelPrint (Printer.hDC)
End Sub


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.