本文介绍了在RichTextBox中所选文本的风格变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何更改样式(如字体,字号,刷机)选中的文本在的RichTextBox
?
How can I change styles (such as Font, FontSize, Brush) of selected Text in RichTextBox
?
更新:比方说,我有一个的RichTextBox
和工具栏。用户进入并选择RichTextBox的方框内的文本,并从工具栏更改字体大小。我想更改选定文本的样式。
Update : Let's say I've a RichTextBox
and a Toolbar. User comes and select text inside the RichTextBox box and change the font size from toolbar. I want to change style of selected text.
推荐答案
WPF
if (this.TextEditor.Selection.IsEmpty)
this.TextEditor.CurrentFontFamily = SelectedFont;
else
this.TextEditor.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, SelectedFont);
或其他WPF示例
private void ChangeTextProperty(DependencyProperty dp, string value)
{
if (mainRTB == null) return;
TextSelection ts = richTextBox.Selection;
if (ts!=null)
ts.ApplyPropertyValue(dp, value);
richTextBox.Focus();
}
下面是一些例子
的Windows
更改字体&安培;字体颜色
here are some examplesWindowsChanging the Font & Font Color
richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold);
richTextBox1.SelectionColor = System.Drawing.Color.Red;
在
another example below
private void WriteTextToRichTextBox()
{
// Clear all text from the RichTextBox;
richTextBox1.Clear();
// Set the font for the opening text to a larger Arial font;
richTextBox1.SelectionFont = new Font("Arial", 16);
// Assign the introduction text to the RichTextBox control.
richTextBox1.SelectedText = "The following is a list of bulleted items:" + "\n";
// Set the Font for the first item to a smaller size Arial font.
richTextBox1.SelectionFont = new Font("Arial", 12);
// Specify that the following items are to be added to a bulleted list.
richTextBox1.SelectionBullet = true;
// Set the color of the item text.
richTextBox1.SelectionColor = Color.Red;
// Assign the text to the bulleted item.
richTextBox1.SelectedText = "Apples" + "\n";
// Apply same font since font settings do not carry to next line.
richTextBox1.SelectionFont = new Font("Arial", 12);
richTextBox1.SelectionColor = Color.Orange;
richTextBox1.SelectedText = "Oranges" + "\n";
richTextBox1.SelectionFont = new Font("Arial", 12);
richTextBox1.SelectionColor = Color.Purple;
richTextBox1.SelectedText = "Grapes" + "\n";
// End the bulleted list.
richTextBox1.SelectionBullet = false;
// Specify the font size and string for text displayed below bulleted list.
richTextBox1.SelectionFont = new Font("Arial", 16);
richTextBox1.SelectedText = "Bulleted Text Complete!";
}
这篇关于在RichTextBox中所选文本的风格变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!