的WinForms文本框 - 使用Ctrl-Backspace键删除整个词 [英] Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word
问题描述
我有其他控件中包含一个文本框,允许输入单行一个WinForms对话框。我想,让用户能够以preSS按Ctrl-Backspace键删除整个单词。这是不是与外的现成文本框的默认行为;我得到的长方形的角色,而不是删除的字。
I have a Winforms dialog that contains among other controls a TextBox that allows a single line of input. I would like to allow the user to be able to press Ctrl-Backspace to delete an entire word. This is not the default behaviour with the out-of-the-box TextBox; I get a rectangle character, rather than having the word deleted.
我已经确认了 ShortcutsEnabled
属性设置为真
。
I have confirmed the ShortcutsEnabled
property is set to True
.
我也发现,我可以用一个RichTextBox,而不是一个TextBox得到我想要的行为。这里的问题是,RichTextBox的(境特别)的外观是从文本框的不同,我并不需要或想要的文字标记的能力。
I did find that I can use a RichTextBox rather than a TextBox to get the behaviour I want. The problem with this is that the apperance of the RichTextBox (border in particular) is different from that of the TextBox, and I don't need or want the ability to mark up text.
所以我的问题是如何最好地处理这种情况?是否有,我很想念文本框的一些财产?或者是它最好使用RichTextBox的,更新的外观,因此是一致的,而文字的禁用标记?
So my question is how to best handle this situation? Is there some property on the TextBox that I am missing? Or is it best to use the RichTextBox, update the appearance so it is consistent, and disable markup of the text?
我是比较快乐写code处理KeyDown和明确地重点preSS事件,如果没有更好的办法,但认为这是值得一试第一。
I am relatively happy to write the code to handle the KeyDown and KeyPress events explicity if there is no better way, but thought it was worth checking first.
推荐答案
我可以模拟按Ctrl + Backspace键发送按Ctrl + Shift +左键和Backspace到文本框。其效果是几乎相同的,而且也没有必要手动处理控制的文本。您可以使用此code ++实现的:
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);
}
}
您还可以修改app.config文件,强制SendKey类使用发送键的较新的方法:
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>
这篇关于的WinForms文本框 - 使用Ctrl-Backspace键删除整个词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!