Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

winforms - How to change the font of multiple sizes in richtextbox in C#?

I have a richtextbox and I would like to make it work like WordPad. My problem is that, for example, if I type "123" using the Calibri font, and then "456" using the Arial font and I want to change the size of 2345 it won't let me to do it because they have two different font types. This is where I have the problem:

private void combo_sizes_TextChanged(object sender, EventArgs e)
{
    if (rtb.SelectionFont == null)
    {
        rtb.SelectionFont = new Font(combo_fonts.Text, Convert.ToInt16(combo_sizes.Text));
    }
    rtb.SelectionFont = new Font(rtb.SelectionFont.FontFamily, Convert.ToInt16(combo_sizes.Text));
}

I know tha when the selected text in the rtb contains multiple fonts the SelectionFont equals to null, so in that case I have made it to get the selected text's size and font from two comboboxes, but I would like to change the size without losing its original font. Is there a solution?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Break your selected text into chars. Get each char's Font, and change its size.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...