question

RamadasRavikumar-2512 avatar image
0 Votes"
RamadasRavikumar-2512 asked RamadasRavikumar-2512 action

Go to next text box when enter is pressed

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);
             }
         }






dotnet-csharpvs-generalwindows-forms
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RamadasRavikumar-2512,
I added TextBox,ComboBox,DateTimePicker to form and made a test with your code.
It worked fine, so what details did I overlook?
Please explain in detail.
Here is test result:
90656-423.gif
Best Regards,
Daniel Zhang


0 Votes 0 ·
423.gif (109.7 KiB)

@DanielZhang-MSFT Enter some text in the textbox , press backspace to clear all , then press enter and check whether it is moving to next control or not.

0 Votes 0 ·

Hi @RamadasRavikumar-2512
I cleared all text via backspace, and it also moved to next control.
Best Regards,
Daniel Zhang

0 Votes 0 ·
Show more comments

1 Answer

DanielZhang-MSFT avatar image
1 Vote"
DanielZhang-MSFT answered RamadasRavikumar-2512 commented

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.



4231.png (16.9 KiB)
4231.gif (96.9 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DanielZhang-MSFT thank you so much it worked!!!!..

0 Votes 0 ·