共用方式為


RichTextBox.SelectionLength 屬性

定義

取得或設定控制項中選取的字元數。

public:
 virtual property int SelectionLength { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public override int SelectionLength { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectionLength : int with get, set
Public Overrides Property SelectionLength As Integer

屬性值

文字方塊中所選取的字元數。

屬性

範例

下列程式碼範例示範如何使用 SelectionLength 屬性來判斷 是否在 內 RichTextBox 選取文字。 此範例要求 RichTextBox 已將名為 的 richTextBox1 控制項新增至表單。 此範例也需要包含 richTextBox1 控制項中選取的文字。

private:
   void ModifySelectedText()
   {
      // Determine if text is selected in the control.
      if ( richTextBox1->SelectionLength > 0 )
      {
         // Set the color of the selected text in the control.
         richTextBox1->SelectionColor = Color::Red;

         // Set the font of the selected text to bold and underlined.
         richTextBox1->SelectionFont = gcnew System::Drawing::Font( "Arial",10,static_cast<FontStyle>(FontStyle::Bold | FontStyle::Underline) );

         // Protect the selected text from modification.
         richTextBox1->SelectionProtected = true;
      }
   }
private void ModifySelectedText()
{
   // Determine if text is selected in the control.
   if (richTextBox1.SelectionLength > 0)
   {
      // Set the color of the selected text in the control.
      richTextBox1.SelectionColor = Color.Red;
      // Set the font of the selected text to bold and underlined.
      richTextBox1.SelectionFont = new Font("Arial",10,FontStyle.Bold | FontStyle.Underline);
      // Protect the selected text from modification.
      richTextBox1.SelectionProtected = true;
   }
}
Private Sub ModifySelectedText()
    ' Determine if text is selected in the control.
    If (richTextBox1.SelectionLength > 0) Then
        ' Set the color of the selected text in the control.
        richTextBox1.SelectionColor = Color.Red
        ' Set the font of the selected text to bold and underlined.
        richTextBox1.SelectionFont = New Font("Arial", 10, FontStyle.Bold Or FontStyle.Underline)
        ' Protect the selected text from modification.
        richTextBox1.SelectionProtected = True
    End If
End Sub

備註

您可以使用這個屬性來判斷文字方塊控制項中目前是否選取任何字元,然後再對選取的文字執行作業。 您也可以使用此屬性來判斷字元總數, (包括迴圈中 for 執行單一字元工作時所選取的空格) 。

適用於

另請參閱