Skip to main content
 首页 » 编程设计

c#之RichTextBox 中的部分文本加粗

2024年02月13日13softidea

我想将 RichTextBox 中的一些 SelectedText 设为粗体。我用过这个:

 private void RichTextBox1_KeyDown(object sender, KeyEventArgs e) 
 { 
     if (e.Modifiers == Keys.Control && e.KeyCode == Keys.B) 
     { 
         if (RichTextBox1.SelectedText.Length == 0) 
         { 
 
         } 
 
         RichTextBox1.SelectedText = Font.Bold; 
     } 
 } 

但是我得到这个错误:

Cannot implicity convert type 'bool' to 'string'.

我不知道该怎么办:\

请您参考如下方法:

Font.Bold返回 boolean 。

Type: System.Boolean 
true if this Font is bold; otherwise, false. 

没有 implicity conversation 从 boolean 到 string。

尝试使用 Font Constructor (Font, FontStyle) ;

RichTextBox1.SelectionFont = new Font(RichTextBox1.Font, FontStyle.Bold);