如何更改富文本框中所选文本或所有文本的字体 [英] How do I change the font of selected text or all text in a rich text box
本文介绍了如何更改富文本框中所选文本或所有文本的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用文本编辑器,我使用的一个按钮是更改richtextbox中所有文本的字体类型和大小,如果你只选择那些文本应该用fontDialog改变的代码。代码我现在可以选择文本并更改所选文本的样式,然后继续使用我开始使用的原始字体类型进行输入。我只需要帮助代码我可以用来更改所有文本,如果没有选择/高亮
我尝试过:
I'm working on a text editor and the one button i use is to change the font type and size of all the text in in a richtextbox and if you select something only that text should change with the fontDialog..The code i have at the moment i can select text and change the selected text's style and then continue typing with the original font type i started with..i just need help with the code i can you use to change all the text if nothing is selected/higlighted
What I have tried:
if (fontDialog1.ShowDialog() == DialogResult.OK & !String.IsNullOrEmpty(rtbText.Text))
{
rtbText.SelectionFont = fontDialog1.Font;
}
推荐答案
您可以在更改SelectedFont之前选择所有文本:
You can select all the text before changing SelectedFont:
rtbText.SelectAll();
rtbText.SelectionFont = fontDialog1.Font;
我过去曾经用过这个;它以与未选择的方式相同的方式处理全选的情况。并且,您可以选择在字体更改后重置选择。
I've used this in the past; it handles the case of all-selected in the same way as none-selected. And, you can, optionally, reset the selection after the Font change.
private void SetFont(RichTextBox rtbx, bool restoreselection = false)
{
int selStart = rtbx.SelectionStart;
int selLength = rtbx.SelectionLength;
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
if (selLength == 0) rtbx.SelectAll();
rtbx.SelectionFont = fontDialog1.Font;
if (restoreselection)
{
rtbx.Focus();
rtbx.SelectionStart = selStart;
rtbx.SelectionLength = selLength;
}
}
}
这篇关于如何更改富文本框中所选文本或所有文本的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文