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