This article concludes with a sample application that uses the RTB to implement a text editor similar to WordPad. The RichTextBox control provides a number of properties you can use to apply formatting to any portion of text within the control. To change the formatting of text, it must first be selected. Only selected text can be assigned character and paragraph formatting.
Using these properties, you can make text bold or italic, change the color, and create superscripts and subscripts. You can also adjust paragraph formatting by setting both left and right indents, as well as hanging indents. Each object inserted into the control is represented by an OLEObject object. This allows you to create documents with the control that contain other documents or objects.
For example, you can create a document that has an embedded Microsoft Excel spreadsheet or a Microsoft Word document or any other OLE object registered on your system.
To insert objects into the RichTextBox control, you simply drag a file from the Windows 95 Explorer for example , or a highlighted portion of a file used in another application such as Microsoft Word , and drop the contents directly onto the control. When an object is pasted in from the clipboard, it is inserted at the current insertion point. When an object is dragged and dropped into the control, the insertion point will track the mouse cursor until the mouse button is released, causing the object to be inserted.
This behavior is the same as Microsoft Word. Applications that already use TextBox controls can easily be adapted to make use of RichTextBox controls. However, the RichTextBox control doesn't have the same 64K character capacity limit of the conventional TextBox control. The RichTextBox control must be added to the toolbox via the Components dialog box, as shown below.
This dialog box is accessed via the Project menu, Components item. Once you check "Microsoft Rich Textbox Control 6. Then you can double-click it to make it appear on your form, as you would with any other control. The Components Dialog Box. Properties of the RichTextBox Control:. Returns or sets a Boolean value that determines if a pop-up menu containing the RTB's verbs Cut, Copy Paste is displayed when the user clicks the RTB control with the right mouse button.
When this property is set to True, Click events and MouseDown events don't occur when the RTB control is clicked with the right mouse button. In order to display your own menus, the AutoVerbMenu property must be set to False. The units for the value is based on the ScaleMode setting twips, pixels, etc. The BulletIndent property returns Null if the selection spans multiple paragraphs with different margin settings.
Returns or sets a Boolean value that determines whether scroll bars in the RichTextBox control are disabled. However, when ScrollBars is set to 1 Horizontal , 2 Vertical , or 3 Both , individual scroll bars are disabled when there are too few lines of text to scroll vertically or too few characters of text to scroll horizontally in the RichTextBox control. Returns or sets the filename of the file loaded into the RichTextBox control. Only the names of text files or valid.
Returns a Boolean value that determines whether selected text appears highlighted when the RTB loses focus. Returns or sets a Boolean value indicating whether the contents of the RTB can be edited. Setting Locked to True makes the contents of the RTB read-only - however, the content can still be modified by code. Returns or sets a value Long indicating whether there is a maximum number of characters a RichTextBox control can hold and, if so, specifies the maximum number of characters.
The default for the MaxLength property is 0, indicating no maximum other than that created by memory constraints on the user's system.
Any number greater than 0 indicates the maximum number of characters. If text that exceeds the MaxLength property setting is assigned to a RichTextBox from code, no error occurs; however, only the maximum number of characters is assigned to the Text property, and extra characters are truncated.
Changing this property doesn't affect the current contents of a RichTextBox, but will affect any subsequent changes to the contents. Returns or sets a Boolean value indicating whether the RTB can accept and display multiple lines of text. If False, carriage returns are ignored and text is restricted to a single line.
A multiple-line RichTextBox control wraps text as the user types text extending beyond the text box. If no horizontal scroll bar is specified, the text in a multiple-line RichTextBox automatically wraps. This property is read-only at run-time. The RightMargin property is a Long value used to set the right most limit for text wrapping, centering, and indentation.
Centering a paragraph is based on the left most part of the text portion doesn't include borders of the RichTextBox control and the RightMargin property. Also, when setting the SelRightIndent property, it will be based on the current setting of the RightMargin property.
The default for the RightMargin property is 0 and will cause the control to set text wrapping equal to the right most part of the RichTextBox control so all text is viewable. Note: Setting this property to a very large value will essentially remove WordWrap. Returns or sets a value indicating whether the RTB has horizontal or vertical scroll bars.
Read-only at run-time. Possible values are 0-rtfNone default, no scroll bars shown , 1-rtfHorizontal horizontal scroll bar only , 2-rtfVertical vertical scroll bar only , or 3-rtfBoth both horizontal and vertical scroll bars shown. Scroll bars are displayed only if the contents of the RichTextBox extend beyond the control's borders. If ScrollBars is set to False, the control won't have scroll bars, regardless of its contents.
A horizontal scrollbar will appear only when the RightMargin property is set to a value that is larger than the width of the control. The value can also be equal to, or slightly smaller than the width of the control. Returns or sets a value that controls the alignment of the paragraphs in a RichTextBox control.
Not available at design time. Possible values are Null the current selection spans more than one paragraph with different alignments , 0-rtfLeft default, the paragraph is aligned along the left margin , 1-rtfRight the paragraph is aligned along the right margin , or 2-rtfCenter the paragraph is centered between the left and right margins.
The SelAlignment property determines paragraph alignment for all paragraphs that have text in the current selection or for the paragraph containing the insertion point if no text is selected. To distinguish between the values of Null and 0 when reading this property at run time, use the IsNull function with the If Else statement.
For example:. Return or set font styles of the currently selected text in a RichTextBox control. The font styles include the following formats: Bold, Italic, Strikethru, and Underline.
Possible values are Null the selection or character following the insertion point contains characters that have a mix of the appropriate font styles , True all the characters in the selection or character following the insertion point have the appropriate font style , or False none of the characters in the selection or character following the insertion point have the appropriate font style. These properties behave like the Bold, Italic, Strikethru, and Underline properties of a Font object.
The RichTextBox control has a Font property and therefore the ability to apply font styles to all the text in the control through the properties of the control's Font object. Use these properties to apply font styles to selected text or to characters entered at the insertion point. Controls strCtl. Do you really need to add a text box to the form, or could you just have a text box, created at design time, that is invisible until it is made visible at run time? You could do that without needing to lock the database exclusively or open the form in design view at run time.
Saturday, June 2, PM. In addition to Dirk's comment, there is also a limit on the number of controls that can be added to a form over it's lifetime. So, it you do it on a regular basis, you'll run into that limit. Sunday, June 3, AM. Many thanks. That does the trick. TextBox Control Introduces the general concepts of the TextBox control, which allows editable, multiline input from the user.
Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Have questions or feedback about Office VBA or this documentation?
Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback. Skip to main content. This browser is no longer supported.
0コメント