简体   繁体   English

如何在Rich Edit控件devexpress中更改所选文本的字体样式

[英]How to change the font style of selected text in rich edit control devexpress

How to change the font style of selected text in rich edit control. 如何在Rich Edit控件中更改所选文本的字体样式。 As in richTextbox we use selection font to do this.But in richText edit control What should i have to done to get same output as richTextBox. 就像在richTextbox中一样,我们使用选择字体来做到这一点。但是在richText编辑控件中,我应该做些什么才能获得与richTextBox相同的输出。

As an example. 举个例子。 In richtextbox we write something and click in the B (bold) button and then the selected textstyle will be Bold. 在richtextbox中,我们编写一些内容,然后单击B(粗体)按钮,然后所选的文本样式将变为粗体。

But I can't do it with rich edit control and button.I have written following code with respect to the Bold button. 但是我无法使用丰富的编辑控件和按钮来实现它。我已经针对粗体按钮编写了以下代码。

editconrol1.Font = new Font(editcontrol1.Font.FontFamily, editcontrol1.Font.Size, FontStyle.Bold)

But the problem is, it changes the whole document as SelectionFont. 但是问题是,它将整个文档更改为SelectionFont。

By clicking bold button, It should change the selected text to Bold and If again click on bold button selected text should change back to normal like MS word 通过单击粗体按钮,应将所选文本更改为粗体,如果再次单击粗体按钮,则所选文本应恢复为正常,如MS word

Kindly review and give feedback. 请审核并提供反馈。

Take a look at the How to: Change Formatting of Selected Text documentation article which demonstrates how you can get the selected text in code, and modify its attributes: 看一下“ 如何:更改所选文本的格式”文档文章,该文章演示了如何在代码中获取所选文本并修改其属性:

CharacterProperties cp = document.BeginUpdateCharacters(document.Selection);
cp.FontName = "Comic Sans MS";
cp.FontSize = 18;
cp.ForeColor = Color.Blue;
cp.BackColor = Color.Snow;
cp.Underline = UnderlineType.DoubleWave;
cp.UnderlineColor = Color.Red;

// Finalize modifications   
// with this method call  
document.EndUpdateCharacters(cp);

The Document.Selection property is used to obtain the DocumentRange object, representing the user selection. Document.Selection属性用于获取代表用户选择的DocumentRange对象。

A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219 完整的示例项目位于https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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