Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 112 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
112
Dung lượng
3,37 MB
Nội dung
Private Sub Command1_Click() RichTextBox1.Text = "This rich text box supports fonts like Arial and_ Courier in different sizes." RichTextBox1.SelStart = RichTextBox1.Find("Arial") RichTextBox1.Span ("Arial") RichTextBox1.SelFontName = "Arial" RichTextBox1.SelFontSize = 24 RichTextBox1.SelStart = 0 RichTextBox1.SelStart = RichTextBox1.Find("Courier") RichTextBox1.Span ("Courier") RichTextBox1.SelFontName = "Courier" RichTextBox1.SelFontSize = 18 End Sub The result appears in Figure 6.11. Figure 6.11 Setting fonts and font sizes. Being able to set the font and font size of individual text selections instead of working with all the text at once in a rich text box is a very powerful capability. Using Bullets In Rich Text Boxes Rich text boxes support bullets, those black dots that appear in lists of items that you want to set off in text. Putting a bullet in front of each item gives the list a snappy appearance and helps the reader assimilate the information quickly. To set bullets, you use the SelBullet and BulletIndent properties. The SelBullet property displays a bullet in front of the paragraph in which the current selection is; the BulletIndent property indicates how much you want the bullet to be indented from the left. TIP: Its a good idea to set the bullet indentation, because if you dont, the bullet will appear right in front of the first character in the paragraph youre bulleting, which can look awkward. Lets make this clearer with an example. We start by placing some text in a rich text box: Private Sub Command1_Click() RichTextBox1.Text = "This rich text box shows how to use bullets _ Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\212-216.html (2 of 4) [3/14/2001 1:36:24 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com and indent bulleted text." We set the indentation for this paragraph to 200 twips: Private Sub Command1_Click() RichTextBox1.Text = "This rich text box shows how to use bullets _ and indent bulleted text." RichTextBox1.SelIndent = 200 Next, we set the bullets indent to 90 twips, so its set off from the rest of the text. We set that indent with the BulletIndent property: Private Sub Command1_Click() RichTextBox1.Text = "This rich text box shows how to use bullets _ and indent bulleted text." RichTextBox1.SelIndent = 200 RichTextBox1.BulletIndent = 90 Finally, we add the bullet with the SelBullet property: Private Sub Command1_Click() RichTextBox1.Text = "This rich text box shows how to use bullets _ and indent bulleted text." RichTextBox1.SelIndent = 200 RichTextBox1.BulletIndent = 90 RichTextBox1.SelBullet = True End Sub Thats itthe result appears in Figure 6.12. Figure 6.12 Adding a bullet to text in a rich text box. Aligning Text In A Rich Text Box You can set the alignment of text in a rich text box paragraph-by-paragraph using the SelAlignment property. You just select the paragraph you want to align, or place the insertion point in that paragraph, and set the SelAlignment property to one of the following values: " rtfLeft0(the default); the paragraph is aligned along the left margin. Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\212-216.html (3 of 4) [3/14/2001 1:36:24 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com " rtfRight1; the paragraph is aligned along the right margin. " rtfCenter2; the paragraph is centered between the left and right margins. Being able to align text paragraph-by-paragraph like this is much more powerful than the simple Alignment property of a standard text box, which aligns all the text at the same time. Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\212-216.html (4 of 4) [3/14/2001 1:36:24 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Setting Text Color In RTF Boxes Another call from the Testing Departmentnow the users want to use different text colors in your word-processing program. Can you do that? Yes, you can, using the SelColor property. To set colors in a rich text box, you just make a selection and set the rich text boxs SelColor property using the RGB() function. You pass three values (each ranging from 0 to 255) to the RGB() function for the three color values: red, green, and blue. Heres an example to make this clearer. We display the text This rich text box supports font colors like red and blue and green. in a rich text box, and color the word red red, blue blue, and green green. Heres how that example looks in code: Private Sub Command1_Click() RichTextBox1.Text = "This rich text box supports font colors like _ red and blue and green." RichTextBox1.SelStart = RichTextBox1.Find("red") RichTextBox1.Span ("red") RichTextBox1.SelColor = RGB(255, 0, 0) RichTextBox1.SelStart = 0 RichTextBox1.SelStart = RichTextBox1.Find("green") RichTextBox1.Span ("green") RichTextBox1.SelColor = RGB(0, 255, 0) RichTextBox1.SelStart = 0 RichTextBox1.SelStart = RichTextBox1.Find("blue") RichTextBox1.Span ("blue") RichTextBox1.SelColor = RGB(0, 0, 255) End Sub This program produces the display you see in Figure 6.13. (Although it only appears in black and white in this book, the word red is red, and so on!) Figure 6.13 Coloring text in a rich text box. Moving The Insertion Point In RTF Boxes Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\216-220.html (1 of 4) [3/14/2001 1:36:46 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Using the UpTo() method, you can move the insertion point around in a rich text box. This method moves the insertion point up to (but not including) a character or set of characters. Moving the insertion point yourself can be a powerful technique in a rich text boxfor example, you can move the insertion point to a section of text the user is searching for. Heres how the UpTo() method works: RichTextBox.UpTo(characterset, forward, negate) The characterset parameter is a string that specifies the set of characters to look for. The forward parameter determines which direction the insertion point moves. The negate parameter specifies whether the characters in characterset define the set of target characters or are excluded from the set of target characters. This is made easier to understand with an example, so lets put together an example now. Here, well display the text Click the button to move the insertion point here: *, and when the user clicks a button, well move the insertion point right up to the asterisk (*). We begin by displaying that text in a rich text box when the form loads: Private Sub Form_Load() RichTextBox1.Text = "Click the button to move the insertion point _ here: *" End Sub Next, when the user clicks a button, we can move the insertion point up to the asterisk in the text this way (note, of course, that you can search for multi-character text as well as single characters): Private Sub Command1_Click() RichTextBox1.UpTo ("*") End Sub Thats not quite good enough, though. Because weve clicked the command button, the button now has the focus, which means the blinking insertion point in the rich text box isnt visible at all. To make sure the insertion point in the rich text box reappears, we give the focus back to the rich text box. This program appears in Figure 6.14. Now were handling the insertion point. Figure 6.14 Moving the insertion point in a rich text box. Private Sub Command1_Click() RichTextBox1.UpTo ("*") RichTextBox1.SetFocus Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\216-220.html (2 of 4) [3/14/2001 1:36:46 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com End Sub Adding Superscripts And Subscripts In Rich Text Boxes Uh ohthe users of your new word-processing program, SuperDuperTextPro, are demanding more text-formatting power. Your program has become so popular that the staff physicists are starting to use it, but they want to use superscripts and subscripts in text. Can you add that? Yes, with the rich text box SelCharOffset property. You use this property to make a selection a superscript or subscriptif you set this value to a positive value, you get a superscript, and if you set it to a negative value, you get a subscript. (All measurements use the measurement units of the underlying form, such as twips.) Lets see an example. Here we can display a simple quadratic equation using this text X12 + 2X1 + 1 = 0 where well make the 1s subscripts and the first 2 a superscript. We start by displaying that text in a rich text box: Private Sub Form_Load() RichTextBox1.Text = "X12 + 2X1 + 1 = 0" End Sub Next, we select the characters we want and set the SelCharOffset property to positive or negative twip values to create superscripts and subscripts: Private Sub Command1_Click() RichTextBox1.UpTo ("1") RichTextBox1.Span ("1") RichTextBox1.SelCharOffset = Ð RichTextBox1.UpTo ("2") RichTextBox1.Span ("2") RichTextBox1.SelCharOffset = 40 RichTextBox1.UpTo ("1") RichTextBox1.Span ("1") RichTextBox1.SelCharOffset = Ð End Sub Thats itthe result of this code appears in Figure 6.15. Now even the physicists will be happy. Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\216-220.html (3 of 4) [3/14/2001 1:36:46 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 6.15 Using superscripts and subscripts in a rich text box. Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\216-220.html (4 of 4) [3/14/2001 1:36:46 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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 ValueDescription 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() Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\220-224.html (1 of 4) [3/14/2001 1:36:58 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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 Youve 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: " rtfRTF0(the default); the RichTextBox control saves its contents as an RTF file. " rtfText1; the RichTextBox control saves its contents as a text file. Heres an example where we display some text in a rich text box: Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\220-224.html (2 of 4) [3/14/2001 1:36:58 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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. 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: " rtfRTF0(The default); the RichTextBox control saves its contents as an RTF file. " rtfText1; the RichTextBox control saves its contents as a text file. 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 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. Visual Basic 6 Black Book:Text Boxes And Rich Text Boxes http://24.19.55.56:8080/temp/ch06\220-224.html (3 of 4) [3/14/2001 1:36:58 AM] Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... http://24.19.55. 56: 8080/temp/ch07\ 235 - 238 .html (3 of 4) [3/ 14/2001 1 :37 :32 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com http://24.19.55. 56: 8080/temp/ch07\ 235 - 238 .html (4 of 4) [3/ 14/2001 1 :37 :32 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons Simpo PDF Merge and Split... http://24.19.55. 56: 8080/temp/ch07\244-247.html (3 of 4) [3/ 14/2001 1 :37 :59 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com http://24.19.55. 56: 8080/temp/ch07\244-247.html (4 of 4) [3/ 14/2001 1 :37 :59 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons Simpo PDF Merge and Split... in Figure 7.13when you click the command button, the SetCaption() subroutine changes its caption, as shown Figure 7. 13 Passing a button to a procedure to change its caption http://24.19.55. 56: 8080/temp/ch07\ 239 -2 43. html (4 of 4) [3/ 14/2001 1 :37 :51 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Handling... type the same name (in the Name property); when you do, Visual Basic will ask if you want to create a control array, as in Figure 7.5 http://24.19.55. 56: 8080/temp/ch07\ 230 - 235 .html (2 of 4) [3/ 14/2001 1 :37 :21 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 7.5 Creating a control array When you... same program The complete code for the form in Figure 7. 16, tourpackages.frm, is located in the tourpackages folder on this books accompaning CD-ROM http://24.19.55. 56: 8080/temp/ch07\247-250.html (3 of 3) [3/ 14/2001 1 :38 :05 AM] Visual Basic 6 Black Book: List Boxes And Combo Boxes Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Chapter 8 List Boxes And Combo Boxes If you need... Box http://24.19.55. 56: 8080/temp/ch08\251-2 53. html (1 of 3) [3/ 14/2001 1 :38 :15 AM] Visual Basic 6 Black Book: List Boxes And Combo Boxes Determining Where An Item Was Added In A Sorted List Box Or Combo Box Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Using Images In Combo Boxes In Depth In this chapter, were going to take a look at two popular Visual Basic controls: list... will move the focus to the next button, and the next, then to the next row, and so on http://24.19.55. 56: 8080/temp/ch07\ 235 - 238 .html (1 of 4) [3/ 14/2001 1 :37 :32 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 7.7 Using tab-enabled buttons TIP: Another use of tab order is in text-entry forms If,... space (and frustrating to the user) http://24.19.55. 56: 8080/temp/ch07\ 235 - 238 .html (2 of 4) [3/ 14/2001 1 :37 :32 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons to display a lot of disabled buttons If you have to disable a lot of buttons, you should hide them and Split Unregistered Version - http://www.simpopdf.com Simpo PDF Merge To make a button disappear, just set its Visible... code: Private Sub Command1_Click() RichTextBox1.UpTo (gstrStringToFind) http://24.19.55. 56: 8080/temp/ch07\ 230 - 235 .html (3 of 4) [3/ 14/2001 1 :37 :21 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons RichTextBox1.SetFocus Simpo End Sub PDF Merge and Split Unregistered Version - http://www.simpopdf.com Now, when the user clicks the button and starts typing again, the focus will... SetCaption Command1 End Sub http://24.19.55. 56: 8080/temp/ch07\ 239 -2 43. html (3 of 4) [3/ 14/2001 1 :37 :51 AM] Visual Basic 6 Black Book: Command Buttons, Checkboxes, And Option Buttons In the SetCaption() procedure, you just declare the button as a parameter; well name that parameter Button and make it ofPDF Merge and Split Unregistered Version - http://www.simpopdf.com Simpo type Control: Private Sub SetCaption(Button . Figure 6. 15. Now even the physicists will be happy. Visual Basic 6 Black Book: Text Boxes And Rich Text Boxes http://24.19.55. 56: 8080/temp/ch 06 2 16- 220.html (3 of 4) [3/ 14/2001 1: 36 : 46 AM] Simpo PDF. ("*") RichTextBox1.SetFocus Visual Basic 6 Black Book: Text Boxes And Rich Text Boxes http://24.19.55. 56: 8080/temp/ch 06 2 16- 220.html (2 of 4) [3/ 14/2001 1: 36 : 46 AM] Simpo PDF Merge and Split Unregistered. aligned along the left margin. Visual Basic 6 Black Book: Text Boxes And Rich Text Boxes http://24.19.55. 56: 8080/temp/ch 06 212-2 16. html (3 of 4) [3/ 14/2001 1: 36 : 24 AM] Simpo PDF Merge and Split Unregistered