Formatting the text in the rich textbox
How to make the 开发者_StackOverflowselected text in a RichTextBox Bold,italic,underline ,change its color to red, Font to "Lucida Sans Unicode" and Text size to 18pt.
Try this
this.richTextBox1.SelectionFont = new Font("Lucida Sans Unicode", 18,
FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
this.richTextBox1.SelectionColor = Color.Red;
I didn't tried it, here is VB.Net code
Me.richTextBox1.SelectionFont = New Font("Lucida Sans Unicode", 18, _
FontStyle.Bold Or FontStyle.Italic Or FontStyle.Underline)
Me.richTextBox1.SelectionColor = Color.Red
Font Bold
Private Sub BoldToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BoldToolStripMenuItem.Click
Try
If Not RichTextBox1.SelectionFont Is Nothing Then
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
Font Italic
Private Sub ItalicToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ItalicToolStripMenuItem.Click
Try
If Not RichTextBox1.SelectionFont Is Nothing Then
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
Font Underline
Private Sub UnderlineToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UnderlineToolStripMenuItem.Click
Try
If Not RichTextBox1.SelectionFont Is Nothing Then
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
Take a FontDialog and ColorDialog from Toolbox
Font Color
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
Font Face
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
精彩评论