Here's an example of a custom UserControl that captures the "Enter" key.
This creates a text box that behaves like the standard text box, but when the user has the cursor in this text box and hits "Enter", it does not trigger the form's OK button.
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Quux.AcadUtilities.Forms
{
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);
}
}
}
}