简体   繁体   中英

c# [Win form ] how to change font size of only selected text in richtextbox

I am creating a text editor in which when i increase the size of font by font dialog box then all font size of rich text box increase instead of only selected text.

How can I change any property of only selected text in text box?

public partial class Form1 : Form
{
    private void change()
    {
        if (click == true)
        {
            FontDialog fd = new FontDialog();

            fd.ShowColor = true;//Show color option in font dialog
            if (fd.ShowDialog() == DialogResult.OK)
            {

                //----------------------> How to affect only selected contents
                richtextbox.ForeColor = fd.Color;
                richtextbox.Font = fd.Font;

            }//end if
        }
    }//end method change
    public Form1()
    {
        InitializeComponent();
    }
    bool click = false;
    private void button1_Click(object sender, EventArgs e)
    {
        click = true;
        change();
    }
}

Based on your code, the following should work for what you need:

richtextbox.SelectionColor = fd.Color;
richtextbox.SelectionFont = fd.Font;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM