It can get tricky. I think you have to trap the ENTER key before the form gets it.
Here's an example of a TextBox that lets you trap the ENTER key:
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Quux.AcadUtilities
{
public class QuuxTextBox : TextBox
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (SuppressEnter)
if (msg.Msg == 256 && keyData == Keys.Enter)
{
OnRaiseEnterKeyPressedEvent(new EventArgs());
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
[Category("Behavior")]
[Description("When set, keeps the ENTER key from triggering the form 'OK' button.")]
public bool SuppressEnter { get; set; }
/// <summary>
/// Fires when 'SuppressEnter' is set to TRUE and user hits ENTER key while editing Textbox.
/// </summary>
[Category("Action")]
[Description("Fires when 'SuppressEnter' is set to TRUE and user hits ENTER key while editing Textbox.")]
public event EventHandler EnterKeyPressed;
protected virtual void OnRaiseEnterKeyPressedEvent(EventArgs e)
{
EventHandler handler = EnterKeyPressed;
if (handler != null)
{
handler(this, e);
}
}
}
}
This textbox would be used in Forms just like a normal textbox, except if you set the "SuppressEnter" property to TRUE, it will "trap" the ENTER keypress, and fire the event instead of letting the Form handle it.