|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Setting The Mouse Pointer In Text Boxes And Rich Text BoxesYou 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.
Searching For (And Replacing) Text In RTF BoxesThe 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.
Saving RTF Files From Rich Text BoxesYouve gotten feedback from a user of your word processor, SuperDuperTextPro, and it seems shes written a 600-page novel with the program and now finds theres 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 easyyou 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:
Heres 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 thats all it takes. Now weve written RTF to a file.
Reading RTF Files Into A Rich Text BoxYou 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:
Heres 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
Thats all there is to itits that easy to load in files. Printing From A Rich Text BoxYou 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. Heres 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, thats the Printer objects 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
|
|
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. |