我有一个Windows窗体多行文本框。 我想使用Shift + Enter而不是使用Enter键在文本框中创建一个新行,传统的Enter键将用于关注下一个控件。 我的意思是Shift + Enter的工作方式与普通多行文本框上的Enter键完全相同(关于文本框maxlength - >所以你不能输入换行符,或者当你插入换行符时当前选中的文本会被删除,...)
我试图覆盖OnKeyDown,在Shift + Enter上插入换行符,但它不能像我预期的那样工作。
I have a Windows Form Multiline Textbox. I want to use Shift+Enter Instead of using Enter key to make a new line in textbox, and the traditional Enter key will be used to focus on the next control. I mean Shift+Enter will work exactly like Enter key on normal multiline textbox (Regard to textbox maxlength --> So you cant enter newline, or current selected text will be delete when you insert newline,...)
I've tried to override OnKeyDown, to insert newline on Shift+Enter, but it doesn't work as I expected.
最满意答案
你需要覆盖OnKeyDown并使用KeyDownEvent的KeyCode属性检查输入密钥。
如果输入被按下并且keydownevent的修饰符属性不等于Keys.Shift,则需要按下按键。 这是关于如何做到这一点的文档:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.suppresskeypress(v=vs.110).aspx
如果按下shift + enter,则需要在光标位置插入Environment.NewLine,然后在插入的Environment.NewLine之后移动光标。 以下是如何做到这一点:
如何在文本框当前光标中粘贴文本?
you'll need to override OnKeyDown and check for enter key using the KeyDownEvent's KeyCode property.
If enter was pressed and the keydownevent's modifiers property does not equal Keys.Shift you'll need to suppress the key press. Here's the documentation on how to do that:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.suppresskeypress(v=vs.110).aspx
If shift+enter was pressed you'll need to insert Environment.NewLine at the cursor position, then move the cursor after the inserted Environment.NewLine. Here's how to do that:
How to paste text in textbox current cursor?
.NET Multiline Textbox使Shift + Enter工作而不是Enter键(.NET Multiline Textbox Make Shift+Enter working instead of Enter key)我有一个Windows窗体多行文本框。 我想使用Shift + Enter而不是使用Enter键在文本框中创建一个新行,传统的Enter键将用于关注下一个控件。 我的意思是Shift + Enter的工作方式与普通多行文本框上的Enter键完全相同(关于文本框maxlength - >所以你不能输入换行符,或者当你插入换行符时当前选中的文本会被删除,...)
我试图覆盖OnKeyDown,在Shift + Enter上插入换行符,但它不能像我预期的那样工作。
I have a Windows Form Multiline Textbox. I want to use Shift+Enter Instead of using Enter key to make a new line in textbox, and the traditional Enter key will be used to focus on the next control. I mean Shift+Enter will work exactly like Enter key on normal multiline textbox (Regard to textbox maxlength --> So you cant enter newline, or current selected text will be delete when you insert newline,...)
I've tried to override OnKeyDown, to insert newline on Shift+Enter, but it doesn't work as I expected.
最满意答案
你需要覆盖OnKeyDown并使用KeyDownEvent的KeyCode属性检查输入密钥。
如果输入被按下并且keydownevent的修饰符属性不等于Keys.Shift,则需要按下按键。 这是关于如何做到这一点的文档:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.suppresskeypress(v=vs.110).aspx
如果按下shift + enter,则需要在光标位置插入Environment.NewLine,然后在插入的Environment.NewLine之后移动光标。 以下是如何做到这一点:
如何在文本框当前光标中粘贴文本?
you'll need to override OnKeyDown and check for enter key using the KeyDownEvent's KeyCode property.
If enter was pressed and the keydownevent's modifiers property does not equal Keys.Shift you'll need to suppress the key press. Here's the documentation on how to do that:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.suppresskeypress(v=vs.110).aspx
If shift+enter was pressed you'll need to insert Environment.NewLine at the cursor position, then move the cursor after the inserted Environment.NewLine. Here's how to do that:
How to paste text in textbox current cursor?
发布评论