格式化富文本框中的文本

时间:2011-02-04 10:54:03

标签: vb.net

如何使RichTextBox中的选定文本粗体,斜体,下划线,将其颜色更改为红色,将Font更改为“Lucida Sans Unicode”,将文本大小更改为18pt。

2 个答案:

答案 0 :(得分:0)

试试这个

this.richTextBox1.SelectionFont = new Font("Lucida Sans Unicode", 18, 
                                           FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
this.richTextBox1.SelectionColor = Color.Red;

我没试过,这里是VB.Net代码

Me.richTextBox1.SelectionFont = New Font("Lucida Sans Unicode", 18, _
                                         FontStyle.Bold Or FontStyle.Italic Or FontStyle.Underline)
Me.richTextBox1.SelectionColor = Color.Red

答案 1 :(得分:0)

字体粗体

Private Sub BoldToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BoldToolStripMenuItem.Click
        Try

         If Not RichTextBox1.SelectionFont Is Nothing Then

            Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont
            Dim newFontStyle As System.Drawing.FontStyle

            If RichTextBox1.SelectionFont.Bold = True Then
                newFontStyle = FontStyle.Regular

            Else
                newFontStyle = FontStyle.Bold

            End If

            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Xor FontStyle.Bold)

        End If

        Catch ex As Exception
            MsgBox(" Bold formatting is not possible", MsgBoxStyle.Information, "Bold")

        End Try
    End Sub

字体斜体

   Private Sub ItalicToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ItalicToolStripMenuItem.Click
        Try
          If Not RichTextBox1.SelectionFont Is Nothing Then

            Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont
            Dim newFontStyle As System.Drawing.FontStyle

            If RichTextBox1.SelectionFont.Italic = True Then
                newFontStyle = FontStyle.Regular

            Else
                newFontStyle = FontStyle.Italic

            End If

            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Xor FontStyle.Italic)

        End If
        Catch ex As Exception
            MsgBox(" Italic formatting is not possible", MsgBoxStyle.Information, "Italic")

        End Try
    End Sub

字体下划线

  Private Sub UnderlineToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UnderlineToolStripMenuItem.Click
        Try
           If Not RichTextBox1.SelectionFont Is Nothing Then

            Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont
            Dim newFontStyle As System.Drawing.FontStyle

            If RichTextBox1.SelectionFont.Underline = True Then
                newFontStyle = FontStyle.Regular

            Else
                newFontStyle = FontStyle.Underline

            End If

            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Xor FontStyle.Underline)

        End If
        Catch ex As Exception
            MsgBox(" Underline formatting is not possible", MsgBoxStyle.Information, "Underline")

        End Try
    End Sub
  

从工具箱中获取FontDialog和ColorDialog

字体颜色

 Private Sub FontColorToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles FontColorToolStripMenuItem1.Click

        ColorDialog1.Color = RichTextBox1.ForeColor

        If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            RichTextBox1.SelectionColor = ColorDialog1.Color

        End If

    End Sub

字体面孔

 Private Sub FontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FontToolStripMenuItem.Click

        If Not RichTextBox1.SelectionFont Is Nothing Then
            FontDialog1.Font = RichTextBox1.SelectionFont
        Else
            FontDialog1.Font = Nothing
        End If

        FontDialog1.ShowApply = False
        FontDialog1.ShowEffects = True


        If FontDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RichTextBox1.SelectionFont = FontDialog1.Font
        End If
    End Sub