Share via


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

Un uso común del control RichTextBox de formularios Windows Forms consiste en dar formato a texto con atributos tales como opciones de fuente o estilos de párrafo. Es posible que la aplicación necesite hacer un seguimiento de los cambios de formato del texto para mostrar una barra de herramientas, como ocurre en muchas aplicaciones de procesamiento de textos.

Para responder a los cambios en los atributos de formato

  • Escriba código en el controlador del evento SelectionChanged para ejecutar la acción adecuada según el valor del atributo. El ejemplo siguiente cambia la apariencia de un botón de la barra de herramientas según el valor de la propiedad SelectionBullet. El botón de la barra de herramientas se actualizará sólo cuando el punto de inserción se desplace al control.

    El ejemplo siguiente supone un formulario con un control RichTextBox y un control ToolBar que contiene un botón de barra de herramientas. Para obtener más información sobre las barras de herramientas y los botones de 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;
          }
       }
    

Vea también

Referencia

SelectionChanged

RichTextBox

Otros recursos

RichTextBox (Control, formularios Windows Forms)

Controles que se utilizan en formularios Windows Forms