简体   繁体   English

RichTextBox相当于TextBox.AcceptsReturn

[英]RichTextBox equivalent of TextBox.AcceptsReturn

I am switching several TextBoxes out for RichTextBoxes to gain some of the cool features. 我正在为RichTextBoxes切换几个TextBox,以获得一些很酷的功能。

I had my TextBoxes configured to AcceptReturn so that the enter key will create a new line, rather than leave the control. 我将TextBox配置为AcceptReturn,以便输入键将创建一个新行,而不是保留控件。 The RichTextBox does not seem to have this feature. RichTextBox似乎没有此功能。

Is there a simple way to do this, or do I have to capture all keypresses and handle them individually? 有一种简单的方法可以做到这一点,还是我必须捕获所有按键并单独处理它们?

Note: This issue occurs only when you set the "AcceptButton" property of the form. 注意:仅当您设置窗体的“AcceptButton”属性时,才会出现此问题。

Set the RichTextBox.AcceptsTab to true. 将RichTextBox.AcceptsTab设置为true。 For some reason this works for both tabs and the enter key. 由于某种原因,这适用于选项卡和回车键。 If you only want the enter keys then you will have to write custom code. 如果您只想输入密钥,则必须编写自定义代码。

The solution is to override IsInputKey : 解决方案是覆盖IsInputKey

protected override bool IsInputKey(Keys keyData)
{
    if (
        (keyData & ~Keys.Modifiers) == Keys.Tab &&
        (keyData & (Keys.Control | Keys.Alt)) == 0
    )
        return false;

    return base.IsInputKey(keyData);
}

After setting AcceptsTab to true, you ensure that the RichTextBox processes both the tab and return key. AcceptsTab设置为true后,确保RichTextBox处理tab和return键。 With the IsInputKey implementation above, we ensure that the Tab and Shift+Tab key never reach the RichTextBox so they are used for navigation instead. 使用上面的IsInputKey实现,我们确保Tab和Shift + Tab键永远不会到达RichTextBox因此它们用于导航。

The above override must be pasted in a class derived from RichTextBox . 上述覆盖必须粘贴在从RichTextBox派生的类中。

Since Carter pointed out that this only applies if AcceptButton is set, and the other solution suggests deriving the RichTextBox class, I found another simple solution. 由于Carter指出这只适用于设置了AcceptButton,而另一个解决方案建议派生RichTextBox类,我找到了另一个简单的解决方案。 Just unset AcceptButton for the time that the/a RichTextBox has the focus. 只是在/ RichTextBox具有焦点的时候取消设置AcceptButton。 Here's a sample code: 这是一个示例代码:

private void RichText_Enter(object sender, EventArgs e)
{
    AcceptButton = null;
}

private void RichText_Leave(object sender, EventArgs e)
{
    AcceptButton = OKActionButton;
}

This assumes that you only have a single AcceptButton and that is unlikely to change. 这假设您只有一个AcceptButton并且不太可能改变。 Otherwise you would have to copy some AcceptButton finding logic here or just backup the previous AcceptButton value before setting it to null. 否则,您必须在此处复制一些AcceptButton查找逻辑,或者在将其设置为null之前备份先前的AcceptButton值。

This solution also has the side effect of removing the default border from the actual accept button, indicating to the user that pressing the Enter key now will not activate that button. 此解决方案还具有从实际接受按钮中删除默认边框的副作用,向用户指示现在按下Enter键将不会激活该按钮。

只需将Richtextbox Property中的Accept选项更改为“true”,它就会像魔术一样工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM