3

Right now I am using some buttons with the following code:

richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold);
richTextBox1.SelectionColor = System.Drawing.Color.Red;

I also want to add some buttons for Bold, Italic, etc using:

richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Italic);

But if I have a Bold option, this code will remove the Bold and add Italic. How can I retain the Bold and Italic?

Thanks!

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
AstraAka
  • 39
  • 1
  • 1
  • 7
  • possible duplicate of [Winforms RichtextBox Bold/Italic/Underline Formatting issue](http://stackoverflow.com/questions/1252040/winforms-richtextbox-bold-italic-underline-formatting-issue) – jruizaranguren Nov 10 '14 at 11:02
  • It is not a duplicate. I want to be able to click "Bold" and add bold to selected text without replacing other styles (such as underline.) – AstraAka Nov 10 '14 at 11:17
  • Possible duplicate of [Substract Flag From FontStyle (Toggling FontStyles) \[C#\]](http://stackoverflow.com/questions/4198429/substract-flag-from-fontstyle-toggling-fontstyles-c) – Jim Fell May 20 '16 at 14:38

5 Answers5

7

This Works for me hope it will help you guys...

private void cmdBold_Click(object sender, EventArgs e)
{
   Font new1, old1;
   old1 = rtxtBox.SelectionFont;
   if (old1.Bold)
      new1 = new Font(old1, old1.Style & ~FontStyle.Bold);
   else
      new1 = new Font(old1, old1.Style | FontStyle.Bold);

   rtxtBox.SelectionFont = new1;
   rtxtBox.Focus();
}

private void cmdItalic_Click(object sender, EventArgs e)
{
  Font new1, old1;
  old1 = rtxtBox.SelectionFont;
  if (old1.Italic)
    new1 = new Font(old1, old1.Style & ~FontStyle.Italic);
  else
    new1 = new Font(old1, old1.Style | FontStyle.Italic);

  rtxtBox.SelectionFont = new1;      

  rtxtBox.Focus();
}

private void cmdUnderline_Click(object sender, EventArgs e)
{
  Font new1, old1;
  old1 = rtxtBox.SelectionFont;
  if (old1.Underline)
    new1 = new Font(old1, old1.Style & ~FontStyle.Underline);
  else
    new1 = new Font(old1, old1.Style | FontStyle.Underline);

  rtxtBox.SelectionFont = new1;
  rtxtBox.Focus();
}

Font Size

private void cmbSize_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == 13)
      {
        if (float.Parse(cmbSize.Text.Trim()) == 0)
        {
          MessageBox.Show("Invalid Font Size, it must be Float Number", "Invalid Font Size", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          cmbSize.Text = "10";
          return;
        }
        if (cmbSize.Text.Trim() != "")
        {
          Font new1, old1;
          old1 = rtxtBox.SelectionFont;
          new1 = new Font(FontFamily.GenericSansSerif, float.Parse(cmbSize.Text.Trim()), old1.Style);
          rtxtBox.SelectionFont = new1;
        }
        rtxtBox.Focus();
      }
    }
Monzur
  • 1,341
  • 14
  • 11
3

What you really need to change both existing and coming text is this:

if (richTextBox2.SelectionLength > 0 ) richTextBox2.SelectionFont =
 new Font(richTextBox1.SelectionFont, FontStyle.Bold | richTextBox1.SelectionFont.Style);
else richTextBox2.Font =
 new Font(richTextBox1.Font, FontStyle.Bold | richTextBox1.Font.Style);

Note that in order to work the selection must not have a mix of styles..

Also you probably ought to use CheckBoxes with Appearence=Button

If you are using those CheckBoxes make sure you don't code their default event CheckedChanged as this would also fire when you set their state in code!

To switch a Style on and off you can use maybe code like this in the Click events of the style boxes:

FontStyle style = checkBox1.CheckState == CheckState.Checked ? 
                    FontStyle.Italic : FontStyle.Regular;

if (richTextBox2.SelectionLength > 0) richTextBox2.SelectionFont =
    new Font(richTextBox1.SelectionFont, style | richTextBox1.SelectionFont.Style);
else richTextBox2.Font =
    new Font(richTextBox1.Font, style | richTextBox1.Font.Style);

This first decides on the new state to set and then set it.

note that I did not use the Checked Property of the CheckBox! To reflect a selection with a mix of bold and non-bold text we need a third state, so the CheckBoxes should have ThreeState=true.

Code to set the states on only two style boxes could look like this:

private void richTextBox2_SelectionChanged(object sender, EventArgs e)
{
   // mixed state:   
   if (richTextBox2.SelectionFont == null)
   {
     checkBox1.CheckState = CheckState.Indeterminate;
     checkBox2.CheckState = CheckState.Indeterminate;
     return;
   }
   checkBox1.Checked =
      (richTextBox2.SelectionFont.Style & FontStyle.Bold) == FontStyle.Bold;
   checkBox2.Checked =
      (richTextBox2.SelectionFont.Style & FontStyle.Italic) == FontStyle.Italic;
}

Starting to look a little larger than you thought at the beginning? Well, it is.. take your time!! (And we haven't even begun with Fonts and sizes ;-)

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Awesome! You answered to my other question also, this is exactly what I needed!!! – AstraAka Nov 10 '14 at 11:43
  • Good. Note that the full code using these Sytle Boxes should include checking the Checked state of all Boxes with every movement of the cursor to always reflect the state of the SelectionFont! – TaW Nov 10 '14 at 11:48
  • I might be missing something (sorry being so noob) I am trying to code an "if checked = true" so when it is checked, it applies the bold, but if its not checked, it wont longer apply bold (and remove bold if selected text.) How can it be done? – AstraAka Nov 10 '14 at 12:05
  • I have added to my answer code to switch on and off and also to set the button states. – TaW Nov 10 '14 at 12:23
  • Thanks :) You rock!! Longer that I though indeed. Applying colors has been easy, this is trickier :) A learning experience! – AstraAka Nov 10 '14 at 12:38
  • Yes, this time mostly about how to combine flags. Note a little correction in the `SelectionChanged`! – TaW Nov 10 '14 at 13:26
1

use this code

richTextBox1.Font = new Font("Tahoma", 12, FontStyle.Bold | FontStyle.Italic);
Hasan Gh
  • 11
  • 2
  • that will always set both. Try `richTextBox1.Font = new Font("Tahoma", 12, FontStyle.Bold | richTextBox1.SelectionFont.Style);` – TaW Nov 10 '14 at 11:27
  • Thanks! I have a total of five buttons: Regular, Bold, Italic, Underline and Strikeout. I want the user to select text and to click on "Bold" and get "Bold" but then, to that selected text, I want the user to be able to add more styles (such as Italic if they desire) without replacing the previous style. Also, it could work if the user clicked a button and the button remained active (like in MS Word) where the user could have none, one or more styles active at a time. I hope I am clear. Thanks a ton for your help! – AstraAka Nov 10 '14 at 11:29
  • Not sure if I can ask follow up here. How about to remove a style (like the same button that adds the style, can remove that style?) Like, active = style, non-active = no style (maybe a button is not the best option? If not, what is?) – AstraAka Nov 10 '14 at 11:41
  • How does the bitwise operator (|) work when creating the new Font? – Stephen Melben Corral Oct 20 '22 at 13:50
1

Sharing a VB.NET Implementation:

Set Bold

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

If Not RichTextBox1.SelectionFont Is Nothing Then

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

    End If

    End Sub

Set Italic

Private Sub ItalicToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ItalicToolStripMenuItem.Click

 If Not RichTextBox1.SelectionFont Is Nothing Then

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

        End If

    End Sub

Set Underline

 Private Sub UnderlineToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UnderlineToolStripMenuItem.Click

  If Not RichTextBox1.SelectionFont Is Nothing Then

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

        End If

    End Sub

RichTextBox SelectionChanged Event

 Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged

 If RichTextBox1.SelectionFont.Bold = True Then

              
                BoldToolStripMenuItem.Checked = True
            Else

              
                BoldToolStripMenuItem.Checked = False
            End If

 If RichTextBox1.SelectionFont.Italic = True Then

           
            ItalicToolStripMenuItem.Checked = True
        Else

          
            ItalicToolStripMenuItem.Checked = False
        End If

 If RichTextBox1.SelectionFont.Underline = True Then

               
                UnderlineToolStripMenuItem.Checked = True
            Else

              
                UnderlineToolStripMenuItem.Checked = False
            End If

End Sub
Mir Rahed Uddin
  • 1,208
  • 2
  • 12
  • 25
0

you need to do something like this..

in order ot make it italic.

 richTextBox1.SelectionFont = new Font("Tahoma", 12, richTextBox1.SelectionFont.Style | FontStyle.Italic);

//in order to make it bold

richTextBox1.SelectionFont = new Font("Tahoma", 12, richTextBox1.SelectionFont.Style | FontStyle.Bold);
sm.abdullah
  • 1,777
  • 1
  • 17
  • 34