Ir para o conteúdo

Richtextbox

Um dos controlos de relevo que o Visual Studio fornece é a Richtextbox, pois tamanhas são as propriedades do mesmo. Em baixo estão alguns dos exemplos básicos mais procurados.

Colocar o texto sublinhado a negrito:

 Private Sub Negrito(ByRef RichText As RichTextBox)
        If RichText.SelectionFont IsNot Nothing Then

            Dim FonteActual As Font = RichText.SelectionFont
            If RichText.SelectionFont.Bold = True Then
                RichText.SelectionFont = New Font(FonteActual, FontStyle.Regular)
            Else
                RichText.SelectionFont = New Font(FonteActual, FontStyle.Bold)
            End If

        End If
    End Sub

Itálico:

Private Sub Italico(ByRef RichText As RichTextBox)
        If RichText.SelectionFont IsNot Nothing Then

            Dim FonteActual As Font = RichText.SelectionFont
            If RichText.SelectionFont.Italic = True Then
                RichText.SelectionFont = New Font(FonteActual, FontStyle.Regular)
            Else
                RichText.SelectionFont = New Font(FonteActual, FontStyle.Italic)
            End If

        End If
    End Sub

Sublinhado:

Private Sub Sublinhado(ByRef RichText As RichTextBox)
        If RichText.SelectionFont IsNot Nothing Then

            Dim FonteActual As Font = RichText.SelectionFont
            If RichText.SelectionFont.Underline = True Then
                RichText.SelectionFont = New Font(FonteActual, FontStyle.Regular)
            Else
                RichText.SelectionFont = New Font(FonteActual, FontStyle.Underline)
            End If

        End If
    End Sub

Rasurado:

Private Sub Rasurado(ByRef RichText As RichTextBox)
        If RichText.SelectionFont IsNot Nothing Then

            Dim FonteActual As Font = RichText.SelectionFont
            If RichText.SelectionFont.Strikeout = True Then
                RichText.SelectionFont = New Font(FonteActual, FontStyle.Regular)
            Else
                RichText.SelectionFont = New Font(FonteActual, FontStyle.Strikeout)
            End If

        End If
    End Sub

Nota: Para chamar os códigos acima, basta colocar o Sub dentro de um evento, ex:

Negrito(RichTextbox1)

Alinhamento à esquerda:

RichTextBox1.SelectionAlignment = HorizontalAlignment.Left

Alinhamento ao centro:

RichTextBox1.SelectionAlignment = HorizontalAlignment.Center

Alinhamento à direita:

RichTextBox1.SelectionAlignment = HorizontalAlignment.Right

Definir a cor do texto:

Private Sub Cor(ByRef RichText As RichTextBox)
        If RichText.SelectionFont IsNot Nothing Then

            Dim EscolherCor As New ColorDialog

            With EscolherCor
                .AllowFullOpen = True
                .AnyColor = True
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    RichText.SelectionColor = .Color
                End If
            End With

        End If
    End Sub

Definir a cor de fundo do texto:

Private Sub CorFundo(ByRef RichText As RichTextBox)
        If RichText.SelectionFont IsNot Nothing Then

            Dim EscolherCor As New ColorDialog

            With EscolherCor
                .AllowFullOpen = True
                .AnyColor = True
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    RichText.SelectionBackColor = .Color
                End If
            End With

        End If
    End Sub