Bagikan melalui


RichTextBox.SelectionLength Properti

Definisi

Mendapatkan atau mengatur jumlah karakter yang dipilih dalam kontrol.

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

Nilai Properti

Jumlah karakter yang dipilih dalam kotak teks.

Atribut

Contoh

Contoh kode berikut menunjukkan cara menggunakan SelectionLength properti untuk menentukan apakah teks dipilih dalam RichTextBox. Contoh ini mengharuskan RichTextBox kontrol, bernama richTextBox1, telah ditambahkan ke formulir. Contoh ini juga mengharuskan teks richTextBox1 berisi teks yang dipilih dalam kontrol.

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

Keterangan

Anda dapat menggunakan properti ini untuk menentukan apakah ada karakter yang saat ini dipilih dalam kontrol kotak teks sebelum melakukan operasi pada teks yang dipilih. Anda juga dapat menggunakan properti ini untuk menentukan jumlah total karakter (termasuk spasi) yang dipilih saat melakukan tugas karakter tunggal dalam perulangan for .

Berlaku untuk

Lihat juga