如何更改富文本框中选定文本的字体

时间:2021-02-28 11:48:40

标签: c# visual-studio textbox

<块引用>

我想更改名为 Notes

的富文本框中选定文本的字体

我尝试使用此代码但它不起作用

Notes.SelectionFont = FontStyle.Italic;

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

试试这个; 选择一个文本然后触发

    private void button1_Click(object sender, EventArgs e)
        {
            int selstart = richTextBox1.SelectionStart;
            int sellength = richTextBox1.SelectionLength;
            richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic);
            richTextBox1.SelectionStart = richTextBox1.SelectionStart + richTextBox1.SelectionLength;
            richTextBox1.SelectionLength = 0;
            richTextBox1.SelectionFont = richTextBox1.Font;
            richTextBox1.Select(selstart, sellength);
        }