Author Topic: using the ENTER key switch to next field  (Read 1818 times)

0 Members and 1 Guest are viewing this topic.

Binky

  • Guest
using the ENTER key switch to next field
« on: August 07, 2008, 11:48:52 AM »
I would like the ENTER key to act like the TAB.  Not sure how to do that

Hoping that there is an easy way, like a property setting or something I am not seeing.

Anybody out there done this???

Thanks

sinc

  • Guest
Re: using the ENTER key switch to next field
« Reply #1 on: August 07, 2008, 02:18:06 PM »
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:

Code: [Select]
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.