Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word
/* Update 2: Please look at https://positivetinker.com/adding-ctrl-a-and-ctrl-backspace-support-to-the-winforms-textbox-control as it fixes all issues with my simple solution */
/* Update 1: Please look also at Damir’s answer below, it’s probably a better solution :) */
I would simulate Ctrl+Backspace by sending Ctrl+Shift+Left and Backspace to the TextBox. The effect is virtually the same, and there is no need to manually process control’s text. You can achieve it using this code:
class TextBoxEx : TextBox
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.Back))
{
SendKeys.SendWait("^+{LEFT}{BACKSPACE}");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
You can also modify the app.config file to force the SendKey class to use newer method of sending keys:
<configuration>
<appSettings>
<add key="SendKeys" value="SendInput" />
</appSettings>
</configuration>
Old question, but I just stumbled upon an answer that doesn't require any extra code.
Enable autocompletion for the textbox and CTRL-Backspace should work as you want it to.
CTRL-Backspace deleting whole word to the left of the caret seems to be a 'rogue feature' of the autocomplete handler. That's why enabling autocomplete fixes this issue.
Source 1 | Source 2
--
You can enable the auto complete feature with setting the AutoCompleteMode
and AutoCompleteSource
to anything you like (for instance; Suggest
and RecentlyUsedList
)