RichTextBox RichTextBox RichTextBox Constructor

定义

初始化 RichTextBox 类的新实例。Initializes a new instance of the RichTextBox class.

public:
 RichTextBox();
public RichTextBox ();
Public Sub New ()

示例

下面的代码示例创建一个RichTextBox控件,该控件将 RTF 文件加载到控件中,并搜索单词 "Text" 的第一个实例。The following code example creates a RichTextBox control that loads an RTF file into the control and searches for the first instance of the word "Text." 然后,代码将更改所选文本的字体样式、字号和字体颜色,并将更改保存回原始文件中。The code then changes the font style, font size, and font color of the selected text and saves the changes back to the original file. 示例代码通过将控件添加到其Form来完成。The example code finishes by adding the control to its Form. 此示例要求将示例代码中创建的方法添加到Form类,并从窗体的构造函数中调用。This example requires that the method created in the example code is added to a Form class and called from the constructor of the form. 该示例还要求在 C 驱动器的根目录中创建一个 RTF 文件,其中包含单词 "Text"。The example also requires that an RTF file is created, in the root of the C drive, containing the word "Text."

public:
   void CreateMyRichTextBox()
   {
      RichTextBox^ richTextBox1 = gcnew RichTextBox;
      richTextBox1->Dock = DockStyle::Fill;

      richTextBox1->LoadFile( "C:\\MyDocument.rtf" );
      richTextBox1->Find( "Text", RichTextBoxFinds::MatchCase );

      richTextBox1->SelectionFont = gcnew System::Drawing::Font(
         "Verdana", 12, FontStyle::Bold );
      richTextBox1->SelectionColor = Color::Red;

      richTextBox1->SaveFile( "C:\\MyDocument.rtf",
         RichTextBoxStreamType::RichText );

      this->Controls->Add( richTextBox1 );
   }
public void CreateMyRichTextBox()
{
    RichTextBox richTextBox1 = new RichTextBox();
    richTextBox1.Dock = DockStyle.Fill;


    richTextBox1.LoadFile("C:\\MyDocument.rtf");
    richTextBox1.Find("Text", RichTextBoxFinds.MatchCase);

    richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
    richTextBox1.SelectionColor = Color.Red;

    richTextBox1.SaveFile("C:\\MyDocument.rtf", RichTextBoxStreamType.RichText);

    this.Controls.Add(richTextBox1);
}

Public Sub CreateMyRichTextBox()
    Dim richTextBox1 As New RichTextBox()
    richTextBox1.Dock = DockStyle.Fill
    
    
    richTextBox1.LoadFile("C:\MyDocument.rtf")
    richTextBox1.Find("Text", RichTextBoxFinds.MatchCase)
    
    richTextBox1.SelectionFont = New Font("Verdana", 12, FontStyle.Bold)
    richTextBox1.SelectionColor = Color.Red
    
    richTextBox1.SaveFile("C:\MyDocument.rtf", RichTextBoxStreamType.RichText)
    
    Me.Controls.Add(richTextBox1)
End Sub

注解

默认情况下, Multiline控件的属性设置为。 trueBy default, the Multiline property of the control is set to true.

适用于

另请参阅