Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Apply Bold, Italic and Underline Formatting to Selected Text in a .NET RichTextBox (Part 2)
DanieldotNET DEC2013 How to programmatically apply more than one font style (bold, italic or underline) to selected text in a RichTextBox control. Software Version: Visual Basic 2008 Express Edition, .NET 3.5
Contents
Introduction The FontStyle Enumeration Combining FontStyle Values Switching off a single FontStyle from a FontStyle Combination Building the Demo Application Running Demo
Introduction
In the first part of this two-part article on RichTextBox font style formatting (Apply Bold, Italic and Underline Formatting to Selected Text in a .NET RichTextBox (Part 1)) it was demonstrated how to apply a single font style to selected text in a RichTextBox. However, the program was unable to apply multiple font styles to the text. This capability is useful in word processing applications, for instance the text below has all three font styles bold, italic and underline applied to it.
RichTextBox
How this could be accomplished is the subject of this second article of the two-part article.
FontStyle also has a FlagsAttribute attribute, which allows it to be treated as a set of flags. Without this attribute, each member value of the enumeration would be mutually exclusive, that is a FontStyle variable would either be Regular, Bold, Italic, Underline, or Strikeout. However, since it is now a set of flags, a variable declared as FontStyle can contain a combination of individual font styles. It can contain Bold Italic, or Bold Italic Underline.
BoldItalic
The bitwise-Or compares each bit position; it produces zero only when both positions are zero, otherwise, it produces one. This new value is now encodes the fact that the new style is a combination of both Bold and Italic. In a similar way, to combine Bold Italic and Underline
Or Or
BoldItalicUnderline
Bitwise-Not simply swaps every 1 with 0, and 0 with 1. Bitwise-And compares the bit positions; if all are ones, it produces a one for that position, otherwise it produces a zero.
2. Put three buttons and a RichTextBox control on Form1. In the Properties Window, change the properties of the controls as follows: Button1 Text = Bold Button2 Text = Italic Button3 Text = Underline RichTextBox1 Font.Size = 14 Form1 Text = Bold Italic Underline Demo 2 The interface should now look like the image below
a. Write the code in the Code Window to resemble the one below. The three event handlers are similar to each other.
Visual Basic Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click Dim style As FontStyle ' Test if there is Bold FontStyling in ' the Selection's FontStyle If RichTextBox1.SelectionFont.Bold Then ' Since Bold Style Exists, undo the Bold Style style = RichTextBox1.SelectionFont.Style _ And Not FontStyle.Bold RichTextBox1.SelectionFont = _ New Font(RichTextBox1.SelectionFont, style) Else ' Since there is no Bold Style, ' apply the Bold Style style = RichTextBox1.SelectionFont.Style _ Or FontStyle.Bold RichTextBox1.SelectionFont = _ New Font(RichTextBox1.SelectionFont, style) End If ' Set input focus on the RichTextBox RichTextBox1.Focus() End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button2.Click Dim style As FontStyle ' Test if there is Italic FontStyling ' in the Selection's FontStyle If RichTextBox1.SelectionFont.Italic Then ' Since Italic Style Exists, undo the Italic Style style = RichTextBox1.SelectionFont.Style _ And Not FontStyle.Italic RichTextBox1.SelectionFont = _ New Font(RichTextBox1.SelectionFont, style) Else ' Since there is no Italic Style, ' apply the Italic Style style = RichTextBox1.SelectionFont.Style _ Or FontStyle.Italic RichTextBox1.SelectionFont = _ New Font(RichTextBox1.SelectionFont, style) End If ' Set input focus on the RichTextBox RichTextBox1.Focus() End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button3.Click Dim style As FontStyle ' Test if there is Underline FontStyling ' in the Selection's FontStyle If RichTextBox1.SelectionFont.Underline Then ' Since Underline Style Exists, undo the Underline Style style = RichTextBox1.SelectionFont.Style _ And Not FontStyle.Underline RichTextBox1.SelectionFont = _ New Font(RichTextBox1.SelectionFont, style) Else ' Since there is no Underline Style, ' apply the Underline Style style = RichTextBox1.SelectionFont.Style _ Or FontStyle.Underline RichTextBox1.SelectionFont = _ New Font(RichTextBox1.SelectionFont, style) End If ' Set input focus on the RichTextBox RichTextBox1.Focus() End Sub End Class The event handler for Button1 is responsible for toggling the Bold style on/off. It checks if the Bold style is on in the font style combination. It does this by getting the Boolean value of the following expression Visual Basic RichTextBox1.SelectionFont.Bold
RichTextBox1s SelectionFont Property has a Bold Property which returns True if the FontStyle of the SelectionFont is Bold, otherwise it returns False. It then uses the value to change the Bold style from On to Off (if True), or Off to On (if False).
The figure shows the text The quick brown fox formatted as The quick brown fox To do it, select quick and press button Bold and button Underline Then, select brown and press button Italic Finally, select the text fox and press button Bold and button Italic