如何使丰富的文本框C#的一些文本加粗 [英] How to make some text bold in a rich text box C#

查看:439
本文介绍了如何使丰富的文本框C#的一些文本加粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个文本编辑器,我可以大胆的,改变颜色等等等等

I want to make a text editor where I can bold, change color etc etc.

我发现这个代码工作大致为:

I found this code to work roughly:

public static void BoldSelectedText(RichTextBox control)
{
     control.SelectionFont = new Font(control.Font.FontFamily, control.Font.Size,         FontStyle.Bold);
}



但是当我尝试在更多字母输入到RTF它仍然是大胆..

But when I try to type in more letters to the rtf it is still bold..

我怎么可以这样做只对选定的文本是要大胆而不会在下一行,除非我选择文本和推大胆按钮。

How can I do so only the selected text is going to bold and the next lines wont unless I select the text and push "Make Bold" button.

推荐答案

您应该选择原来的字体后,设置字体。

You should set the font after the selection to the original font.

control.SelectionFont = new Font(control.Font, FontStyle.Bold);
control.SelectionStart = control.SelectionStart + control.SelectionLength;
control.SelectionLength = 0;
control.SelectionFont = control.Font;

如果你愿意,你可以保存SelectionStart和SelectionLength并调用选择功能再次选择文本。

If you want you can save the SelectionStart and SelectionLength and call the Select function to select the text again.

int selstart = control.SelectionStart;
int sellength = contorl.SelectionLength;
// ... previous code
control.Select(selstart, sellength);



这样的文字保持选定的,事后的字体仍然是正确的。

this way the text stays selected, and the font afterwards is still correct

这篇关于如何使丰富的文本框C#的一些文本加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆