Compartir a través de


Cómo determinar cuándo cambian los atributos de formato en el control RichTextBox de Windows Forms

Un uso común del control de Formularios Windows Forms RichTextBox es dar formato al texto con atributos como opciones de fuente o estilos de párrafo. Es posible que la aplicación tenga que realizar un seguimiento de los cambios en el formato de texto para mostrar una barra de herramientas, como en muchas aplicaciones de procesamiento de texto.

Para responder a los cambios en los atributos de formato

  1. Escriba código en el SelectionChanged controlador de eventos para realizar una acción adecuada en función del valor del atributo. En el ejemplo siguiente se cambia la apariencia de un botón de barra de herramientas en función del valor de la SelectionBullet propiedad . El botón de la barra de herramientas solo se actualizará cuando se mueva el punto de inserción en el control .

    El ejemplo que sigue supone un formulario con un control RichTextBox y un control ToolBar que tiene una botonera de herramientas. Para obtener más información sobre las barras de herramientas y los botones de la barra de herramientas, vea Cómo: Agregar botones a un control ToolBar.

    ' The following code assumes the existence of a toolbar control
    ' with at least one toolbar button.
    Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged
       If RichTextBox1.SelectionBullet = True Then
          ' Bullet button on toolbar should appear pressed
          ToolBarButton1.Pushed = True
       Else
           ' Bullet button on toolbar should appear unpressed
           ToolBarButton1.Pushed = False
       End If
    End Sub
    
    // The following code assumes the existence of a toolbar control
    // with at least one toolbar button.
    private void richTextBox1_SelectionChanged(object sender,
    System.EventArgs e)
    {
       if (richTextBox1.SelectionBullet == true)
       {
          // Bullet button on toolbar should appear pressed
          toolBarButton1.Pushed = true;
       }
       else
       {
          // Bullet button on toolbar should appear unpressed
          toolBarButton1.Pushed = false;
       }
    }
    
    // The following code assumes the existence of a toolbar control
    // with at least one toolbar button.
    private:
       System::Void richTextBox1_SelectionChanged(
          System::Object ^  sender, System::EventArgs ^  e)
       {
          if (richTextBox1->SelectionBullet == true)
          {
             // Bullet button on toolbar should appear pressed
             toolBarButton1->Pushed = true;
          }
          else
          {
             // Bullet button on toolbar should appear unpressed
             toolBarButton1->Pushed = false;
          }
       }
    

Consulte también