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);
            }
        }
Developer technologies | Windows Forms
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | 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.
{count} votes

Answer accepted by question author
  1. Daniel Zhang-MSFT 9,661 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

Your answer

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