question

truonghoai avatar image
0 Votes"
truonghoai asked truonghoai commented

Winform - Button - Prevent button pass Enter key to other control.

I have a textbox1 and some more textbox with keypress event that change Enter key to Tab.
I have a button with click event: {textbox1.Focus();}.
The problem is that, when I press Enter key to fire button's Click event, the textbox1 gets focusing and cursor moves to next control (because textbox keypress event). It work well if I click on button by mouse.
Now I want pressing enter key on button without passing Enter key value to other controls.
Please help

windows-forms
· 4
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.


Probably you can adjust the code for keypress event that changes Enter key to Tab.


0 Votes 0 ·

Please see clearly my problem. The problem is not textbox keypress issue, but the button keypress issue. Tell me how to prevent key buffer - that will be sent to the next focus control - the reason causes that issue. Again, When I presss enter on the button (key press), the Enter Key will be sent to Textbox1 if I set Textbox1.Focus(); in Button_Click; That's mean, I press Enter on Button, Button send Enter to Textbox after fire Click Event.

0 Votes 0 ·

Probably you can adjust some code that is not shown.


0 Votes 0 ·
Show more comments
Castorix31 avatar image
0 Votes"
Castorix31 answered truonghoai commented

Instead of using Keypress events, you can use ProcessCmdKey to handle keys globally

For example, to go to the next control on Enter, but only when the focus is not on the button (button1 in my test),
you can do something like

     protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
     {
         if (keyData == Keys.Enter)
         {  
             if (this.ActiveControl.Handle != button1.Handle)
                 this.SelectNextControl(this.ActiveControl, true, true, true, true);             
         }
         return base.ProcessCmdKey(ref msg, keyData);
     }

· 9
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.

Thanks @Castorix31,
However, I've already try it and it does not work. If you don't mind, give me your email, I will send you my sample so that you can understand the situation.

0 Votes 0 ·

I don't see what does not work
For example, if I do :

     private void button1_Click(object sender, EventArgs e)
     {
         MessageBox.Show("Information", "Button click");
         textBox1.Focus();
     }   


then when I type [Enter] and I reach the Button, it shows the MessageBox, then it goes on the first TextBox and so on :

191583-processcmdkey.gif


0 Votes 0 ·
processcmdkey.gif (202.3 KiB)

Hi @Castorix31, You are misunderstading my situation.
All textbox is custom control, that I've added EnterMoveNext.
And when I presss Enter on button1, it also send Enter Key to textbox1. That will make the cursor does not stop at textbox1 and move to next control.
Now I want to stop the Enter Key Passing after press key on Button, Could you help?

0 Votes 0 ·

Don't use code in TextBoxes as ProcessCmdKey replaces it to navigate to next control

0 Votes 0 ·

Use ProcessCmdKey will cause "Ding". I don't want my app "Ding"

0 Votes 0 ·
Show more comments
truonghoai avatar image
0 Votes"
truonghoai answered Castorix31 commented

Hi @Castorix31 , Thanks for your support.
However, On the message box, could you presss ENTER instead of CLICK. Show me the result please!

· 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.

Yes, the result is the same

0 Votes 0 ·