c#加粗代码,如何使丰富的文本框C#的一些文本加粗

本文介绍了如何在 richtextbox 控件中实现仅对选定文本进行加粗操作,而不影响后续输入的文字。关键在于在设置选中文字为粗体后,需恢复选区之后的字体为原始样式,以确保新输入的内容不会被错误地格式化。
摘要由CSDN通过智能技术生成

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);

}

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;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值