Use enter key for submission

rahul kumar 605 Reputation points
2023-06-24T12:16:50.44+00:00

I have created a customtextbox which on pressing enter moves to next control (in tab order) . Now i have used many textboxes on a single form (form which acts as a data entry form ) . Now i am looking for a way in which when the user reaches last of these custom textboxes , fills the data in it ( last textbox ) and presses enter , I can submit the all textboxes data to my database .

 public partial class customtextbox : TextBox
    {
        
        public customtextbox()
        {          
             InitializeComponent();
        }
        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
           
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
         
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

                if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
                {
                    SendKeys.Send("{TAB}");

                    //disable beep 
                    e.Handled = e.SuppressKeyPress = true;
                }
                if ((e.KeyCode == Keys.Back || e.KeyCode == Keys.Up) && this.Text.Length == 0)
                {
                    SendKeys.Send("+{TAB}");
                }
            
        }
}


I don't want any button or hidden button on my Form . Another problem is if I used the form keydown event to capture enter key and set the keypreview property to true and do my desired thing , the above code from the customtextbox

SendKeys.Send("{TAB}");

doesn't work as the form's keydown event eats it up . Please suggest me a way to get this when the focus is inside the last customtextbox .

Developer technologies Windows Forms
Developer technologies C#
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-06-24T19:48:23.08+00:00

    Here is a quick-n-dirty sample missing moving backwards.

    using System.ComponentModel;
    using System.Diagnostics;
    
    #pragma warning disable CS8618
    
    namespace WinFormsApp1;
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            Controls
                .OfType<NoBeepTextBox>()
                .ToList()
                .ForEach(b => { b.TriggerEvent += OnTriggerEvent; });
        }
    
        private void OnTriggerEvent(bool last, string name)
        {
            if (last)
            {
                Debug.WriteLine($"Button {name} call to database save method");
            }
        }
    }
    
    /// <summary>
    /// Place in a class file
    /// </summary>
    public class NoBeepTextBox : TextBox
    {
        /// <summary>
        /// We really do not need name, here just to show you the correct
        /// TextBox was used
        /// </summary>
        public delegate void TriggerDelegate(bool isLast, string name);
        public event TriggerDelegate TriggerEvent;
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == (Keys.Return))
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
    
                TriggerEvent?.Invoke(Last, Name);
    
                if (!Last)
                {
                    SendKeys.Send("{TAB}");
                }
                
                return;
            }
    
            base.OnKeyDown(e);
        }
    
        /// <summary>
        /// Set the last TextBox to true in the form designer
        /// </summary>
        [Category("Behavior"), Description("Last TextBox")]
        public bool Last { get; set; }
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.