Etrit
Etrit

Reputation: 905

Partial Text in RichTextBox to Bold

I want to make some of the SelectedText in a RichTextBox Bold. I used this :

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

But I get this error :

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

I don't know what to do :\

Upvotes: 1

Views: 2555

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98868

Font.Bold returns boolean .

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

There is no implicity conversation from boolean to string.

Try with using Font Constructor (Font, FontStyle);

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

Upvotes: 3

Davio
Davio

Reputation: 4737

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

instead of RichTextBox1.SelectedText = Font.Bold;

Font.Bold is a boolean property indicating whether the current Font is bold. In this case the current Font refers to this.Font or the main Font for the control in which the RichtTextBox is located.

RichTextBox1.SelectedText is a string so that's why it doesn't work. You don't need to set the SelectedText, but the SelectionFont.

Upvotes: 1

Related Questions