Go to next text box when enter is pressed

Ramadas Ravikumar 46 Reputation points
2021-04-22T05:23:16.427+00:00

Hello in my WinForms application i have this below program which helps to Go to next text box when enter key is pressed , But i am facing an issue here .
When I press enter key after I type something or without typing anything in first textbox it is going to next text box.
But if I type something accidentally and hit backspace to erase the first textbox content and if I press enter key then it is not moving to next textbox , Kindly help how to solve this issue.

private void Form1_Shown(object sender, EventArgs e)
        {
            try
            {
                foreach (Control control in Controls)
                {
                    if (control is TextBox textBox)
                    {
                        textBox.KeyDown += (s, ea) =>
                        {
                            if (ea.KeyCode != Keys.Enter) return;
                            ea.SuppressKeyPress = true;
                            SelectNextControl(ActiveControl, true, true, true, true);
                        };
                    }
                    else if (control is ComboBox comboBox)
                    {
                        comboBox.KeyDown += (s, ea) =>
                        {
                            if (ea.KeyCode != Keys.Enter) return;
                            ea.SuppressKeyPress = true;
                            SelectNextControl(ActiveControl, true, true, true, true);
                        };
                    }
                    else if (control is DateTimePicker dateTimePicker)
                    {
                        dateTimePicker.KeyDown += (s, ea) =>
                        {
                            if (ea.KeyCode != Keys.Enter) return;
                            ea.SuppressKeyPress = true;
                            SelectNextControl(ActiveControl, true, true, true, true);
                        };
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Enter Failed:" + ex.Message.ToString(), "Enter",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,821 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,575 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,199 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-04-23T08:55:23.09+00:00

    Hi RamadasRavikumar-2512,
    I suggest you change the CausesValidation property of your fieldTextBox to false which suppresses validating and validated events.
    90644-4231.png
    And I made a test, it worked fine.
    The test result:
    90703-4231.gif
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful