Trả lời mọi thắc mắc của bạn

Liệt kê những bài viết mới nhất

Bài viết mớiNgười viếtVào lúc
paltalk code HdAd
Sun Mar 06, 2016 1:28 am
vb ListView HdAd
Sun Mar 06, 2016 1:00 am
Find occurent of character in string nth HdAd
Sat Jan 16, 2016 10:48 pm
Send listbox items to textbox HdAd
Wed Jan 13, 2016 9:55 pm
[VB] Find word in listbox items HdAd
Wed Jan 13, 2016 7:55 pm
code cho hint2 HdAd
Wed Jan 13, 2016 3:22 pm
[vb] Font RTB HdAd
Tue Dec 29, 2015 10:29 am
[vb] selected text RTB HdAd
Tue Dec 29, 2015 9:06 am
[vb] send unicode text HdAd
Tue Dec 29, 2015 9:03 am
[vb] format multi richtextbox HdAd
Tue Dec 29, 2015 9:01 am
Trang web tai cac bang sang che HdAd
Mon Dec 28, 2015 1:28 pm
[VB] Sử dụng resource của chính chương trình HdAd
Sat Dec 26, 2015 8:56 pm
[vb] Phân biệt class và Module HdAd
Thu Dec 24, 2015 2:39 pm
[VB] MY API and Functions declaration HdAd
Mon Dec 21, 2015 1:45 pm
[vb] Khai báo và sử dụng hàm Setforeground HdAd
Mon Dec 21, 2015 1:29 pm
[VB] Sử dụng hàm Sendmessage để gửi tiếng việt, và format richtextbox HdAd
Mon Dec 21, 2015 1:26 pm
[VB] Find menu context handle/ID by SPY++ HdAd
Fri Dec 18, 2015 1:20 pm
[VB] Find menu context handle/ID - Vu Hai Ninh HdAd
Fri Dec 18, 2015 1:00 pm
[VB] Find menu context handle/ID HdAd
Fri Dec 18, 2015 12:54 pm
[VB] Sleep in VB program HdAd
Thu Dec 17, 2015 7:48 pm
[VB] CLose window if found HdAd
Wed Dec 16, 2015 7:07 pm
[VB] Không cho thay đổi kích cỡ form khi chạy HdAd
Wed Dec 16, 2015 3:28 pm
[VB] Không cho thay đổi text box khi chạy HdAd
Wed Dec 16, 2015 3:22 pm
[VB] Tìm cửa sổ và Button bằng FindWindow & FindWindowsEx HdAd
Wed Dec 16, 2015 3:16 pm
[VB] Xóa text mặc định của text box khi click HdAd
Wed Dec 16, 2015 3:12 pm

You are not connected. Please login or register

[vb] selected text RTB

View previous topic View next topic Go down  Message [Page 1 of 1]

1 [vb] selected text RTB on Tue Dec 29, 2015 9:06 am


The RichTextBox control provides properties for manipulating the selected text on the control. The names of these properties start with the Selection or Selected prefix, and the most commonly used ones are shown in Table 4.5. Some of these properties are discussed in further detail in following sections.


The SelectedText property represents the selected text, whether it was selected by the user via the mouse or from within your code. To assign the selected text to a variable, use the following statement:
You can also modify the selected text by assigning a new value to the SelectedText property. The following statement converts the selected text to uppercase:
RichTextbox1.SelectedText =
You can assign any string to the SelectedText property. If no text is selected at the time, the statement will insert the string at the location of the pointer.
Table 4.5 - RichTextBox Properties for Manipulating Selected Text
PropertyWhat It Manipulates
SelectedTextThe selected text
SelectedRtfThe RTF code of the selected text
SelectionStartThe position of the selected text’s first character
SelectionLengthThe length of the selected text
SelectionFontThe font of the selected text
SelectionColorThe color of the selected text
SelectionBackColorThe background color of the selected text
SelectionAlignmentThe alignment of the selected text
SelectionIndent, SelectionRightIndent, SelectionHangingIndentThe indentation of the selected text
RightMarginThe distance of the text’s right margin from the left edge of the control
SelectionTabsAn array of integers that sets the tab stop positions in the control
SelectionBulletWhether the selected text is bulleted
BulletIndentThe amount of bullet indent for the selected text

SelectionStart, SelectionLength

To simplify the manipulation and formatting of the text on the control, two additional properties, SelectionStart andSelectionLength, report (or set) the position of the first selected character in the text and the length of the selection, respectively, regardless of the formatting of the selected text. One obvious use of these properties is to select (and highlight) some text on the control:
RichTextBox1.SelectionStart = 0
RichTextBox1.SelectionLength = 100
You can also use the Select method, which accepts as arguments the starting location and the length of the text to be selected.


Use this property to read or change the alignment of one or more paragraphs. This property’s value is one of the members of the HorizontalAlignment enumeration: Left, Right, and Center. Users don’t have to select an entire paragraph to align it; just placing the pointer anywhere in the paragraph will do the trick, because you can’t align part of the paragraph.

SelectionIndent, SelectionRightIndent, SelectionHangingIndent

These properties allow you to change the margins of individual paragraphs. The SelectionIndent property sets (or returns) the amount of the text’s indentation from the left edge of the control. The SelectionRightIndent property sets (or returns) the amount of the text’s indentation from the right edge of the control. The SelectionHangingIndent property indicates the indentation of each paragraph’s first line with respect to the following lines of the same paragraph. All three properties are expressed in pixels.
The SelectionHangingIndent property includes the current setting of the SelectionIndent property. If all the lines of a paragraph are aligned to the left, the SelectionIndent property can have any value (this is the distance of all lines from the left edge of the control), but the SelectionHangingIndent property must be zero. If the first line of the paragraph is shorter than the following lines, the SelectionHangingIndent has a negative value. Figure 4.18 shows several differently formatted paragraphs. The settings of the SelectionIndent and SelectionHangingIndent properties are determined by the two sliders at the top of the form.

Figure 4.18 - Various combinations of the SelectionIndent and SelectionHangingIndent properties produce all possible paragraph styles.

SelectionBullet, BulletIndent

You use these properties to create a list of bulleted items. If you set the SelectionBullet property to True, the selected paragraphs are formatted with a bullet style, similar to the <ul> tag in HTML. To create a list of bulleted items, select them from within your code and assign the value True to the SelectionBullet property. To change a list of bulleted items back to normal text, make the same property False.
The paragraphs formatted as bullets are also indented from the left by a small amount. To set the amount of the indentation, use the BulletIndent property, which is also expressed in pixels.


Use this property to set the tab stops in the RichTextBox control. The Selection tab should be set to an array of integer values, which are the absolute tab positions in pixels. Use this property to set up a RichTextBox control for displaying tab-delimited data.

Methods Of the RichTextBox control

The first two methods of the RichTextBox control you need to know are SaveFile and LoadFile. The SaveFile method saves the contents of the control to a disk file, and the LoadFile method loads the control from a disk file.


The syntax of the SaveFile method is as follows:
RichTextBox1.SaveFile(path, filetype)
where path is the path of the file in which the current document will be saved. By default, the SaveFile method saves the document in RTF format and uses the .RTF extension. You can specify a different format by using the second optional argument, which can take on the value of one of the members of the RichTextBoxStreamType enumeration, described in Table 4.6.
Table 4.6 - The RichTextBoxStreamType Enumeration
PlainTextStores the text on the control without any formatting
RichNoOLEObjsStores the text without any formatting and ignores any embedded OLE objects
RichTextStores the text in RTF format (text with embedded RTF commands)
TextTextOLEObjsStores the text along with the embedded OLE objects
UnicodePlainTextStores the text in Unicode format


Similarly, the LoadFile method loads a text or RTF file to the control. Its syntax is identical to the syntax of the SaveFile method:
RichTextBox1.LoadFile(path, filetype)
The filetype argument is optional and can have one of the values of the RichTextBoxStreamType enumeration. Saving and loading files to and from disk files is as simple as presenting a Save or Open common dialog to the user and then calling one of the SaveFile or LoadFile methods with the filename returned by the common dialog box.

Select, SelectAll

The Select method selects a section of the text on the control, similar to setting the SelectionStart andSelectionLength properties. The Select method accepts two arguments: the location of the first character to be selected and the length of the selection:
RichTextBox1.Select(start, length)
The SelectAll method accepts no arguments and it selects all the text on the control.

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum