Rich Text Box
Total Page:16
File Type:pdf, Size:1020Kb
Rich Text Box Introduction Using Rich Text Box you can create Notepad or WordPad. Means in rich text box you can give special effects like change font style, size, color, put bullets and change alignments of text. Means using rich text box you can create text file. You can also open and modify existing text file in it. Adding an Rich Text Format Box to a Form So you’ve decided to make the move from text boxes to rich text boxes, and you turn to the toolbox. Wait a minute-where’s the Rich Text Box tool in the toolbox? The answer is that it’s not there until you add it. To add a rich text box to a form, follow these steps: ● Select the Project Ø Components menu item. ● Click the Controls tab in the Components box. ● Find and select the Microsoft Rich Textbox Control box, and click on OK to close the Components box. ● The rich text control now appears in the toolbox and you can use it to add rich text boxes to your forms. R What these steps really accomplish is to add the Richtx32.ocx file to your program, and you’ll need to distribute that file with your program if you use rich text boxes. Accessing Text In a Rich Text Box To access text in a rich text box, you can use two properties: Text and TextRTF. As their names imply, Text holds the text in a rich text box in plain text format (like a text box), and TextRTF holds the text in Rich Text Format. Here’s an example where we read the text in RichTextBox1 without any RTF codes and display that text as plain text in RichTextBox2 : Private Sub Command1_Click () RichTextBox2.Text = RichTextBox1.Text End Sub Here’s the same operation where we transfer the text including all RTF codes that is, here we’re transferring rich text from one rich text box to another: 123 Private Sub Command1_Click () RichTextBox2.TextRTF = RichTextBox1.TextRTF End Sub Selecting Text in Rich Text Boxes Rich text boxes support the SelText property just like standard text boxes. However, SelText only works with plain text. You can set the start and end of the plain-text selection with the SelStart and SelLength properties. If you want to work with RTF-selected text, on the other hand, use the SelRTF property. For example, here’s how we select the first 10 characters in RichTextBox1 and transfer them to RichTextBox2 using SelRTF : Private Sub Command1_Click() RichTextBox1.SelStart = 0 RichTextBox1.SelLength = 10 RichTextBox2.TextRTF = RichTextBox1.SelRTF End Sub The Span Method Besides the SelRTF property, you can use the Span() method to select text based on a set of characters : RichTextBox.Span 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 form the set of target characters. You use Span() to extend a selection from the current insertion point based on a set of specified characters. This method searches the text in the rich text box (forwards or backwards as you’ve specified) and extends the text selection to include (or exclude, if you’ve so specified) as many of the characters you’ve specified in the character set that it can find. For example, to select the text from the current insertion point to the end of the sentence, use Span(“.?!”), which works for sentences ending in periods, question marks, or exclamation marks. Here’s an example where we use Span() to find the word “underlined” and underline it: Private Sub Command1_Click() RichTextBox1.Text = “This rich text supports underlined, bold, italic, and strikethru text.” RichTexBox1.SelStart = RichTextBox1.Find (“underlined”) RichTextBox1.Span (“underlined”) RichTextBox1.SelUnderline = True End Sub 124 Using Bold, Italic, Underline and Strikethru in RTF To make text bold, italic, underline, and strikethru, you see the SelBold, SelItalic, SelUnderline, and SelStrikethru properties. These properties work on selected RTF text only, so you have to select the text whose format you want to change. To make this clearer, here’s an example where we set the underline, bold, italic, and strikethru properties of text. We start by placing some text into a rich text box: Private Sub Command1_Click() RichTextBox1.Text = “This rich text box supports underlined, bold, italic and strikethru text.” … Next, we’ll underline the word “underlined” in the text. We start by finding that word using the rich text box Find() method : Private Sub Command1_Click() RichTextBox1.Text = “This rich text box supports underlined, bold, italic and strikethru text.” RichTextBox1.SelStart=RichTextBox1.Find(“underlined”) … We then use Span() to select the word “underlined” : Private Sub Command1_Click() RichTextBox1.Text = “This rich text box supports underlined, bolditalic, and strikethru text.” RichTextBox1.SelStart=RichTextBox1.Find(“underlined”) RichTextBox1.Span (“underlined”) … Finally we underline the selected text by setting the rich text box’s SelUnderline property to True : Private Sub Command1_Click() RichTextBox1.Text = “This rich text box supports underlined, bold, italic and strikethru text.” RichTextBox1.SelStart=RichTextBox1.Find(“underlined”) RichTextBox1.Span (“underlined”) RichTextBox1.SelUnderline = True … And we can do the same to demonstrate bold, italic, and strikethru text : Private Sub Command1_Click() RichTextBox1.Text = “This rich text box supports underlined, bold, italic and strikethru text.” RichTextBox1.SelStart=RichTextBox1.Find(“underlined”) RichTextBox1.Span (“underlined”) RichTextBox1.SelStart = 0 125 RichTextBox1.SelStart = RichTextBox1.Find(“bold”) RichTextBox1.Span (“bold”) RichTextBox1.SelBold = True RichTextBox1.SelStart = 0 RichTextBox1.SelStart =RichTextBox1.Find(“italic”) RichTextBox1.Span (“italic”) RichTextBox1.SelItalic = True RichTextBox1.SelStart = 0 RichTextBox1.SelStart=RichTextBox1.Find(“strikethru”) RichTextBox1.Span (“strikethru”) RichTextBox1.SelStrikeThru = True End Sub Running this program yields the results you see as follows. Indenting Text In Rich Text Boxes One of the aspects of word processor that users have been used to is the ability to indent text, and rich text boxes (which are designed to be RTF word processors in control) have this capability. To indent paragraph-by-paragraph, you use these properties (you set them to numeric values to indicate the indentation amount, using the measurement units of the underlying form, which is usually twips): ● SelIndent – Indents the first line of the paragraph ● SelHengingIndent – Indents all other lines of paragraph with respect to SelIndent ● SelRightIndent – Sets the right indentation of the paragraph To use these properties on a paragraph of text, you either select the paragraph (using SelStart and SelLength, or Span()), or simply place the insertion point in the paragraph ( you can move the insertion point under program control with the UpTo() method). When the user places the insertion point in a paragraph of text and clicks a button, command1, we can indent the paragraph 500 twips. We can then outdent all lines after the first by 250 twips with respect to the overall 500-twip indentation (which means that all lines after the first will be indented 250 twips from the left margin) and set the right indent to 100 twips : 126 Private Sub Command1_Click() RichTextBox1.Text = “This rich text box displays very good example of indentation from both side” RichTextBox1.SelIndent = 500 RichTextBox1.SelHangingIndent = 250 RichTextBox1.SelRightIndent = 100 End Sub Now we’re indenting individual paragraphs in rich text controls. Besides working paragraph-by-paragraph, you can set the right margin for the whole rich text at once with the RightMargin property. Just assign this property the new value you want for the right margin, and you’re set. Setting Fonts and Font Sizes in RTF To set a selection’s font, you just set the SelFontName to the new font name (for example, Arial or Times New Roman). To set a selection’s font size, you just set the SelFontSize property. That’s all it takes. Here’s an example. In this case, we’ll display the text “This rich text box supports fonts like Arial and Courier in different sizes.” In a rich text box, and format the words “Arial” and “Courier” in those fonts, and in different font sizes. We start by placing that text in a rich text box : Private Sub Command1_Click () RichTextBox1.Text = “This rich text box supports fonts like Arial and Courier in different sizes.” … Next, we select the word “Arial” : Private Sub Command1_Click () RichTextBox1.Text = “This rich text box supports fonts like Arial and Courier in different sizes.” 127 RichTextBox1.SelStart = RichTextBox1.Find (“Arial “ ) RichTextBox1.Span (“Arial”) … Then we display that word in Arial font, with a 24-point size: 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.SelFontSize = 24 … We do the same for “Courier”, displaying it in 18-point size : Private Sub Command1_Click() RichTextBox1.Text = “This rich text box supports fonts like Arial and Courier New in different sizes.” RichTextBox1.SelStart = RichTextBox1.Find(“Arial “) RichTextBox1.Span (“Arial”) RichTextBox1.SelFontSize = 15 RichTextBox1.SelStart = 0 RichTextBox1.SelStart = RichTextBox1.Find(“Courier New “) RichTextBox1.Span (“Courier New”) RichTextBox1.SelFontName = “Courier New “ RichTextBox1.SelFontSize = 18 End Sub 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. Output of the above code will be as shown below Using Bullets in RTF Rich text boxes support bullets, those black dots that appear in lists of items that you want to set off in 128 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 SetBullet and BulletIndent properties.