Compartir a través de


Procedimiento para determinar cuándo cambian los atributos de formato en el control RichTextBox de formularios Windows Forms

Un uso común del control RichTextBox de Windows Forms 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 con el fin de mostrar una barra de herramientas, como en muchas aplicaciones de procesamiento de texto.

Responder a los cambios en los atributos de formato

  1. Escriba código en el controlador de eventos SelectionChanged 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 propiedad SelectionBullet. El botón de la barra de herramientas solo se actualizará cuando se mueva el punto de inserción en el control.

    En el ejemplo siguiente se 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 la barra de herramientas, consulte Cómo: Agregar botones a un control Barra de herramientas.

    ' 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