I am switching several TextBoxes out for RichTextBoxes to gain some of the cool features.
I had my TextBoxes configured to AcceptReturn so that the enter key will create a new line, rather than leave the control. The RichTextBox does not seem to have this feature.
Is there a simple way to do this, or do I have to capture all keypresses and handle them individually?
The RichTextBox is similar to the TextBox, but it has additional formatting capabilities. Whereas the TextBox control allows the Font property to be set for the entire control, the RichTextBox allows you to set the Font, as well as other formatting properties, for selections within the text displayed in the control.
In This Article. Gets or sets whether an end-user can insert return characters into a text. This is a dependency property. Namespace: DevExpress.UI.Xaml.Editors.
A RichTextBox is a better choice when it is necessary for the user to edit formatted text, images, tables, or other rich content. For example, editing a document, article, or blog that requires formatting, images, etc is best accomplished using a RichTextBox.
With the RichTextBox control, the user can enter and edit text. The control also provides more advanced formatting features than the standard TextBox control. Text can be assigned directly to the control, or can be loaded from a rich text format (RTF) or plain text file.
Note: This issue occurs only when you set the "AcceptButton" property of the form.
Set the RichTextBox.AcceptsTab to 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.
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. Just unset AcceptButton for the time that the/a RichTextBox has the focus. 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. Otherwise you would have to copy some AcceptButton finding logic here or just backup the previous AcceptButton value before setting it to null.
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.
The solution is to override 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. 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.
The above override must be pasted in a class derived from RichTextBox
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With