[英]C# - Change font of text in RichTextBox dynamically?
I am having some text in a "richTextBox" and a "comboBox" having names of some fonts. 我在“richTextBox”和“comboBox”中有一些文本,其中包含一些字体的名称。 I want to change the font of text in "richTextBox" if a new font is selected from the "comboBox".
如果从“comboBox”中选择了新字体,我想更改“richTextBox”中的文本字体。 I am using following code.
我正在使用以下代码。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 1)
richTextBox1.Font = new Font("Comic Sans MS", 14);
}
The problem is that if I select the font, the text does not change its font automatically, it only changes if I type some new text. 问题是,如果我选择字体,文本不会自动更改其字体,只有在我键入一些新文本时才会更改。 I also tried
richTextBox1.SelectionFont
instead of richTextBox1.Font
. 我也尝试过
richTextBox1.SelectionFont
而不是richTextBox1.Font
。 I also added InputTextBox.Refresh();
我还添加了
InputTextBox.Refresh();
after the above code to refresh the text box but in vein. 在上面的代码之后刷新文本框但是静脉。
How I can change font of the text by just selecting from comboBox? 如何通过从comboBox中选择来更改文本的字体?
Update: I just figured out that above code is fine, the problem is that I was using wrong event call, used comboBox1_SelectedValueChanged()
in place of comboBox1_SelectedIndexChanged()
and it works fine now. 更新:我刚刚发现上面的代码很好,问题是我使用了错误的事件调用,使用
comboBox1_SelectedValueChanged()
代替comboBox1_SelectedIndexChanged()
,它现在工作正常。
Tip: If you want to change font of entire TextBox use richTextBox1.Font
, if you want to change font of selected text only use richTextBox1.SelectionFont
. 提示:如果要更改整个TextBox的字体,请使用
richTextBox1.Font
,如果要更改所选文本的字体,请仅使用richTextBox1.SelectionFont
。
You could select all the text before changing SelectedFont
option: 您可以在更改
SelectedFont
选项之前选择所有文本:
this.richTextBox1.SelectAll();
this.richTextBox1.SelectionFont = newFont;
You have to iterate throughout your text for that. 你必须在整个文本中进行迭代。 This is a method it might help you:
这是一种可能对您有所帮助的方法:
private void ChangeFontStyleForSelectedText(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
{
_maskChanges = true;
try
{
int txtStartPosition = txtFunctionality.SelectionStart;
int selectionLength = txtFunctionality.SelectionLength;
if (selectionLength > 0)
using (RichTextBox txtTemp = new RichTextBox())
{
txtTemp.Rtf = txtFunctionality.SelectedRtf;
for (int i = 0; i < selectionLength; ++i)
{
txtTemp.Select(i, 1);
txtTemp.SelectionFont = RenderFont(txtTemp.SelectionFont, familyName, emSize, fontStyle, enableFontStyle);
}
txtTemp.Select(0, selectionLength);
txtFunctionality.SelectedRtf = txtTemp.SelectedRtf;
txtFunctionality.Select(txtStartPosition, selectionLength);
}
}
finally
{
_maskChanges = false;
}
}
If you want to see how I did this you can read this article: http://how-to-code-net.blogspot.ro/2014/01/how-to-make-custom-richtextbox-control.html Good luck ;) 如果你想看看我是怎么做到的,你可以阅读这篇文章: http : //how-to-code-net.blogspot.ro/2014/01/how-to-make-custom-richtextbox-control.html祝你好运;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.