lost_in_the_source
lost_in_the_source

Reputation: 11237

How to change font of text?

I already know how to change the font:

    private void toolStripButton2_Click(object sender, EventArgs e)//italic
    {
        //maintext is the richTextBox
        maintext.SelectionFont = new Font(maintext.Font, FontStyle.Italic);
        maintext.SelectionStart = maintext.SelectionStart + maintext.SelectionLength;
        maintext.SelectionLength = 0;
        maintext.SelectionFont = maintext.Font;
    }

But how do I allow for two fonts at the same time, and make the font back to normal? And also so that you don't have to first type the text and then select it; just press the button.

Upvotes: 0

Views: 123

Answers (1)

user2480047
user2480047

Reputation:

You can define as many "selection chunks" as you wish an associate a different font to each of them. Sample code making the font style of the first half of the text italics and the second half bold.

maintext.SelectionStart = 0;
maintext.SelectionLength = maintext.Text.Length / 2;
maintext.SelectionFont = new Font(maintext.Font, FontStyle.Italic);

maintext.SelectionStart = maintext.Text.Length / 2;
maintext.SelectionLength = maintext.Text.Length - maintext.Text.Length / 2;
maintext.SelectionFont = new Font(maintext.Font, FontStyle.Bold);

maintext.SelectionStart = maintext.Text.Length;
maintext.SelectionFont = new Font(maintext.Font, FontStyle.Regular);
maintext.SelectionLength = 0;

Upvotes: 1

Related Questions