How can I get VB.net that ENTER creates a new line and doesn't close the dialog?
I created a dialog box with RichTextBox and I would like to have the following behavior:
I have focus (cursor) in RichTextBox. When the ENTER key is pressed, a new control line should appear in the extended revision. ENTER should NOT close the dialog [as is done now :-(].
Any idea?
a source to share
If the AcceptButton property is set while referencing a button on a form, this will intercept the enter key presses. Make sure there is no AcceptButton assigned on the form and the textbox should receive input keystrokes and behave as expected.
Update. If you want to have AcceptButton behavior and receive a RichTextBox when the enter key is pressed while focusing, you can achieve this in two different ways. One is to set the form's KeyPreview property to true and add the following code to the form's KeyPress event handler:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter && this.ActiveControl != theRichTextBox)
{
this.DialogResult = DialogResult.OK;
}
}
Another approach is to assign the AcceptButton property to the button (which in turn has the DialogResult property set to OK
). Then you can add event handlers for the Enter and Leave events for the RichTextBox control, which temporarily does not assign the form's AcceptButton property:
private void RichTextBox_Enter(object sender, EventArgs e)
{
this.AcceptButton = null;
}
private void RichTextBox_Leave(object sender, EventArgs e)
{
this.AcceptButton = btnAccept;
}
a source to share