Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RichTextBox equivalent of TextBox.AcceptsReturn

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?

like image 475
captncraig Avatar asked Jul 16 '10 19:07

captncraig


People also ask

What is difference between RichTextBox and TextBox?

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.

What is Acceptsreturn?

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.

When should we use RichTextBox?

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.

What is a rich TextBox?

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.


3 Answers

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.

like image 107
Carter Medlin Avatar answered Oct 17 '22 18:10

Carter Medlin


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.

like image 34
ygoe Avatar answered Oct 17 '22 19:10

ygoe


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.

like image 3
Pieter van Ginkel Avatar answered Oct 17 '22 19:10

Pieter van Ginkel