다음을 통해 공유


ControlExtensions.AddRichTextBox Method

Definition

Overloads

AddRichTextBox(ControlCollection, Range, String)

Adds a new RichTextBox control to the worksheet at the range specified.

AddRichTextBox(ControlCollection, Double, Double, Double, Double, String)

Adds a new RichTextBox control to the worksheet in the specified size and location.

AddRichTextBox(ControlCollection, Range, String)

Adds a new RichTextBox control to the worksheet at the range specified.

public:
[System::Runtime::CompilerServices::Extension]
 static Microsoft::Office::Tools::Excel::Controls::RichTextBox ^ AddRichTextBox(Microsoft::Office::Tools::Excel::ControlCollection ^ controls, Microsoft::Office::Interop::Excel::Range ^ range, System::String ^ name);
public static Microsoft.Office.Tools.Excel.Controls.RichTextBox AddRichTextBox (this Microsoft.Office.Tools.Excel.ControlCollection controls, Microsoft.Office.Interop.Excel.Range range, string name);
static member AddRichTextBox : Microsoft.Office.Tools.Excel.ControlCollection * Microsoft.Office.Interop.Excel.Range * string -> Microsoft.Office.Tools.Excel.Controls.RichTextBox
<Extension()>
Public Function AddRichTextBox (controls As ControlCollection, range As Range, name As String) As RichTextBox

Parameters

controls
ControlCollection

The collection to add the control to. Do not supply this parameter yourself. When you call this method on the collection returned by the Controls property (in an application-level project) or the Controls property (in a document-level project), this parameter is supplied automatically.

range
Range

A Range that provides the bounds for the control.

name
String

The name of the control that can be used to index the control in the ControlCollection instance.

Returns

The RichTextBox control that was added to the ControlCollection instance.

Exceptions

The name or range argument is null, or the name argument has zero length.

A control with the same name is already in the ControlCollection instance.

The range that was specified is not valid. Multi-area ranges cannot be used. The range should be on the same worksheet as the ControlCollection instance.

Examples

The following code example adds a RichTextBox control to cells A1 through B2, assigns text to the control, and then formats the text. To use this example, run it from the Sheet1 class in a document-level project.

private void ExcelRangeAddRichTextBox()
{

    Microsoft.Office.Tools.Excel.Controls.RichTextBox
         richTextBox1 = this.Controls.AddRichTextBox(
         this.Range["A1", "B2"], "richTextBox1");
    richTextBox1.Text = "Sample text";
    richTextBox1.Select(0, 6);
    richTextBox1.SelectionFont = new Font("Times New Roman",
        10, FontStyle.Bold);
}
Private Sub ExcelRangeAddRichTextBox()

    Dim RichTextBox1 As Microsoft.Office.Tools. _
        Excel.Controls.RichTextBox = Me.Controls. _
        AddRichTextBox(Me.Range("A1", "B2"), _
        "RichTextBox1")

    RichTextBox1.Text = "Sample text"
    RichTextBox1.Select(0, 6)
    RichTextBox1.SelectionFont = New Font( _
        "Times New Roman", 10, FontStyle.Bold)

End Sub

Remarks

The AddRichTextBox method enables you to add RichTextBox objects to the end of the ControlCollection. To remove a RichTextBox that was previously added programmatically, use the Remove method.

The control automatically resizes when the range is resized.

Applies to

AddRichTextBox(ControlCollection, Double, Double, Double, Double, String)

Adds a new RichTextBox control to the worksheet in the specified size and location.

public:
[System::Runtime::CompilerServices::Extension]
 static Microsoft::Office::Tools::Excel::Controls::RichTextBox ^ AddRichTextBox(Microsoft::Office::Tools::Excel::ControlCollection ^ controls, double left, double top, double width, double height, System::String ^ name);
public static Microsoft.Office.Tools.Excel.Controls.RichTextBox AddRichTextBox (this Microsoft.Office.Tools.Excel.ControlCollection controls, double left, double top, double width, double height, string name);
static member AddRichTextBox : Microsoft.Office.Tools.Excel.ControlCollection * double * double * double * double * string -> Microsoft.Office.Tools.Excel.Controls.RichTextBox
<Extension()>
Public Function AddRichTextBox (controls As ControlCollection, left As Double, top As Double, width As Double, height As Double, name As String) As RichTextBox

Parameters

controls
ControlCollection

The collection to add the control to. Do not supply this parameter yourself. When you call this method on the collection returned by the Controls property (in an application-level project) or the Controls property (in a document-level project), this parameter is supplied automatically.

left
Double

The distance in points between the left edge of the control and the left edge of the worksheet.

top
Double

The distance in points between the top edge of the control and the top edge of the worksheet.

width
Double

The width of the control in points.

height
Double

The height of the control in points.

name
String

The name of the control.

Returns

The RichTextBox control that was added to the ControlCollection instance.

Exceptions

The name argument is null or has zero length.

A control with the same name is already in the ControlCollection instance.

Examples

The following code example adds a RichTextBox control to the top of the worksheet, assigns text to the control, and then formats the text. To use this example, run it from the Sheet1 class in a document-level project.

private void ExcelRangeAddRichTextBox()
{

    Microsoft.Office.Tools.Excel.Controls.RichTextBox
         richTextBox1 = this.Controls.AddRichTextBox(
         this.Range["A1", "B2"], "richTextBox1");
    richTextBox1.Text = "Sample text";
    richTextBox1.Select(0, 6);
    richTextBox1.SelectionFont = new Font("Times New Roman",
        10, FontStyle.Bold);
}
Private Sub ExcelRangeAddRichTextBox()

    Dim RichTextBox1 As Microsoft.Office.Tools. _
        Excel.Controls.RichTextBox = Me.Controls. _
        AddRichTextBox(Me.Range("A1", "B2"), _
        "RichTextBox1")

    RichTextBox1.Text = "Sample text"
    RichTextBox1.Select(0, 6)
    RichTextBox1.SelectionFont = New Font( _
        "Times New Roman", 10, FontStyle.Bold)

End Sub

Remarks

The AddRichTextBox method enables you to add RichTextBox objects to the end of the ControlCollection. To remove a RichTextBox control that was previously added programmatically, use the Remove method.

Applies to