If the form's AcceptButton property is assigned, referencing a button on the form, this will intercept the enter key presses. Make sure that the form does not have any AcceptButton assigned, and the text box should receive the enter key presses and behave as expected.
Update: If you want to have the behaviour of the AcceptButton and have the RichTextBox receiving the enter key presses when focused you can achieve this in two different ways. One is to set the KeyPreview property of the form to true, and adding the following code to the KeyPress event handler of the form:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter && this.ActiveControl != theRichTextBox)
{
this.DialogResult = DialogResult.OK;
}
}
The other approach is to have the AcceptButton property assigned, pointing out a button (that in turn has its DialogResult property set to OK
). Then you can add event handlers for the Enter and Leave events for the RichTextBox control that will temporarily un-assign the AcceptButton property of the form:
private void RichTextBox_Enter(object sender, EventArgs e)
{
this.AcceptButton = null;
}
private void RichTextBox_Leave(object sender, EventArgs e)
{
this.AcceptButton = btnAccept;
}