Deleting text from a RichTextBox
The RichTextBox does not provide a Delete function like it does for Cut(), Copy(), and Paste(). From what I have found on the Internet to delete text you set the SelectedText equal to an empty string. While this works, the UndoActionName of the RichTextBox is "Unknown", why?
My question is, is setting SelectedText equal to an empty string really the correct way of deleting text in a RichTextBox and if so, how can I set the UndoActionName to "Delete"?
My code:
// get selected text in the text box
string selectedText = richTextBox.SelectedText;
if (selectedText.Length > 0)
richTextBox.SelectedText = "";
// otherwise, if not the end of the document, remove the first character after the caret
else
{
int caretPosition = richTextBox.SelectionStart;
if (caretPosition != richTextBox.TextLength)
{
richTextBox.Select(caretPosition, 1);
richTextBox.SelectedText = "";
}
}
|