winforms.net-3.0

Italicizing characters in a RichTextBox


I found how to make text bold in code:

richTextBox1.Rtf = @"{\rtf1\ansi This is in \b bold\b0.}";

But I also need how to make text italic. Google doesn't give me much.

I tried this (similar to bold, but with different character) but that doesn't work.

richTextBox1.Rtf = @"{\rtf1\ansi This is in \i italic\i0.}";

Can someone help me out please?


Solution

  • this is how I managed to do it:

    richTextBox1.Rtf = @"{\rtf1\ansi This is in \i\f0\fs17 italic\i0.}";
    

    edit:

    How did I do this? I created a small test-application with a richtextbox and a button.

    I typed some text in the richtextbox, I selected the text and pressed the button.

    private void button1_Click(object sender, EventArgs e)
    {
        richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic);
        richTextBox1.SaveFile(@"c:\test.rtf");
    }
    

    This saved the rtf. I opened the rtf in Notepad++. The content of the rtf was

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\i\f0\fs17 hello\i00\par
    }
    

    and that's how I found how to use italics in a richtextbox.