Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning all and sorry for deleting the question in the first place, I acidentally hit the wrong button when trying to modify it... Simon_Whale and Killzone DeathMan please accept my applogy.
I was wondering if it is possible to use the KeyDown in a textbox without triggering the AcceptButton assigned to the form?

On the form there is a textbox that stores some text (thats surprising).
If the user presses "Enter" within the textbox I need to perform some validation.
When validation fails it is not allowed to close the form.

So I added the KeyDown to the textbox where I check for the "Enter" key.

So far so good.

Unfortunately if somebody presses "Enter" in the textbox the AcceptButton is triggered before I can handle the KeyDown...

Is there a way to do this without removing the AcceptButton assignement?

I could change the assignement of the AcceptButton upon the Focus events of the textbox... but somehow that doesn't feel right

[UPDATE: Currently (I really hoped it would be a workaround only) I set the AcceptButton of the form to null when the textbox receives focus and set it back to the related button when the textbox looses focus again.
While this actually works I do feel dirty... somehow :)]

Any input is highly appreciated,
best regards
Andy
Posted
Updated 26-May-16 7:04am
v3
Comments
hoernchenmeister 30-Nov-12 5:16am    
@Simon_Whale
Unfortunately the KeyPressed event is not triggered before the AcceeptButton fires...
hoernchenmeister 30-Nov-12 5:19am    
@Killzone DeathMan
Ok, let me explain a little bit more.
When I am in the textbox (where I listen to the KeyDown event and check for KeyCode == Keys.Enter) and I actually hit the enter key the ButtonClick event of the assigned AcceptButton (AcceptButton Property of the form) is fired before the KeyDown event in the textbox...
Pete O'Hanlon 30-Nov-12 5:48am    
What technology are you using? WinForms or WPF?
hoernchenmeister 30-Nov-12 5:50am    
Hi Pete,
I actually use WinForms... still :)

Well, just spitballing here, but the issue is that you need to suppress the AcceptButton when a particular field is invalid. Seems to me that the easiest option here is to disable the button that's set and only enable it when validation has passed. A refinement would be to remove the AcceptButton altogether when the particular TextBox has focus, and then reenable it when the validation passes or when the TextBox loses focus.
 
Share this answer
 
Comments
hoernchenmeister 30-Nov-12 7:12am    
I havent thought of disabling the button on validation.
As mentioned I already set the AcceptButton property to null if the textbox has focus and reset it if it looses focus, but I just hoped there would me a more elegant way to deal with this problem...
Somehow I assumed I could trap the KeyDown and handle it before the AcceptButton fires... which was wrong obviously... :)
Next thing is that the textbox is actually on a usercontrol on the form and the AcceptButton is on the form itself, which requires visibility of elements in the usercontrol that I normally do not like to be visible to the outer world...
Thanks Pete for your answer, it is very much appreciated.
May you too have a great weekend ;)
Its not great news I'm afraid, according to this SO post thats how it works

Trouble with form AcceptButton[^]

After reading this MSDN Article MSDN : Forms.AcceptButton[^]

the only thing that overrides the accept button is a control that accepts the enter key as "normal" behaviour such as multiline text boxs or RichTextBox.

Sorry mate I don't know what to suggest from reading these articles.
 
Share this answer
 
Comments
hoernchenmeister 30-Nov-12 5:49am    
Thank you Simon, even if this seems to be bad news :)
Currently (I really hoped it would be a workaround only) I set the AcceptButton of the form to null when the textbox receives focus and set it back to the related button when the textbox looses focus again.
While this actually works I do feel dirty... somehow :)
Anyway, thanks for trying to help me out and again, I am really sorry for deleting the first post...
cheers and have a great weekend
Andy
Simon_Whale 30-Nov-12 5:58am    
Not a problem Andy have a good weekend too :-D
Use the Validating event of TextBox.

It checks if your validation condition is met before leaving the control. For example
C#
private void textBox1_Validating(object sender, CancelEventArgs e)
{
    if (textBox1.Text == "hello")
        e.Cancel = true;
}


If the user types 'hello' in textBox1, the AcceptButton click event is not raised and this way, you don't have to change anything. Just change the validation in the KeyDown event to the Validating event.
 
Share this answer
 
Comments
Simon_Whale 30-Nov-12 6:03am    
The OP was after capturing a keyress event rather than validating the actual text, as far as I am aware validating doesn't allow you to test the keypressed
hoernchenmeister 30-Nov-12 6:12am    
Simon is right on this one.
While the Validating event indeed fires before, I can not test if the enter key is pressed. Even logging the keys won't work because "Enter" again activates the AcceptButton.
Anyway, thanks for your suggestion GeekBond

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900