Freigeben über


RichTextBox.SelectionLength-Eigenschaft

Ruft die Anzahl der im Steuerelement markierten Zeichen ab oder legt diese fest.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Overrides Property SelectionLength As Integer
'Usage
Dim instance As RichTextBox
Dim value As Integer

value = instance.SelectionLength

instance.SelectionLength = value
public override int SelectionLength { get; set; }
public:
virtual property int SelectionLength {
    int get () override;
    void set (int value) override;
}
/** @property */
public int get_SelectionLength ()

/** @property */
public void set_SelectionLength (int value)
public override function get SelectionLength () : int

public override function set SelectionLength (value : int)

Eigenschaftenwert

Die Anzahl der markierten Zeichen im Textfeld.

Hinweise

Sie können mit dieser Eigenschaft bestimmen, ob derzeit im Textfeld-Steuerelement Zeichen markiert sind, bevor Sie den markierten Text verarbeiten. Sie können mit dieser Eigenschaft auch die Gesamtanzahl von Zeichen (einschließlich Leerzeichen) festlegen, die markiert werden, wenn Tasks für einzelne Zeichen in einer for-Schleife ausgeführt werden.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie mit der SelectionLength-Eigenschaft ermittelt wird, ob in der RichTextBox Text ausgewählt wurde. Für dieses Beispiel ist es erforderlich, dass dem Formular ein RichTextBox-Steuerelement mit dem Namen richTextBox1 hinzugefügt wurde. Für das Beispiel ist es außerdem erforderlich, dass richTextBox1 Text enthält, der im Steuerelement ausgewählt wurde.

Private Sub WriteTextToRichTextBox()
   ' Clear all text from the RichTextBox;
   richTextBox1.Clear()
   ' Set the font for the opening text to a larger Arial font;
   richTextBox1.SelectionFont = New Font("Arial", 16)
   ' Assign the introduction text to the RichTextBox control.
   RichTextBox1.SelectedText = "The following is a list of bulleted items:" + ControlChars.Cr
   ' Set the Font for the first item to a smaller size Arial font.
   richTextBox1.SelectionFont = New Font("Arial", 12)
   ' Specify that the following items are to be added to a bulleted list.
   richTextBox1.SelectionBullet = True
   ' Set the color of the item text.
   richTextBox1.SelectionColor = Color.Red
   ' Assign the text to the bulleted item.
   richTextBox1.SelectedText = "Apples" + ControlChars.Cr
   ' Apply same font since font settings do not carry to next line.
   richTextBox1.SelectionFont = New Font("Arial", 12)
   richTextBox1.SelectionColor = Color.Orange
   richTextBox1.SelectedText = "Oranges" + ControlChars.Cr
   richTextBox1.SelectionFont = New Font("Arial", 12)
   richTextBox1.SelectionColor = Color.Purple
   richTextBox1.SelectedText = "Grapes" + ControlChars.Cr
   ' End the bulleted list.
   richTextBox1.SelectionBullet = False
   ' Specify the font size and string for text displayed below bulleted list.
   richTextBox1.SelectionFont = New Font("Arial", 16)
   richTextBox1.SelectedText = "Bulleted Text Complete!"
End Sub
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:
   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.get_SelectionLength() > 0) {
        // Set the color of the selected text in the control.
        richTextBox1.set_SelectionColor(Color.get_Red());
        // Set the font of the selected text to bold and underlined.
        richTextBox1.set_SelectionFont(new Font("Arial", 10, FontStyle.
            Bold | FontStyle.Underline));
        // Protect the selected text from modification.
        richTextBox1.set_SelectionProtected(true);
    }
} //ModifySelectedText

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

RichTextBox-Klasse
RichTextBox-Member
System.Windows.Forms-Namespace
SelectionStart
SelectedText