acceptbutton

G

Guest

Hello, I'm working on a set of forms inherited from the windows form. Within
these forms I often use keyevents to perform certain functions i.e.
'return/enter in a textbox' to submit a value to a routine, perform a search
or just to send a tab.

As I have no one button on the form that I would always want to fire a click
when I hit enter I have the acceptbutton set to nothing. This causes the
obvious problem of the windows default beep sounding everytime the key is
pressed.

I have attempted to change the inherited form, setting a default button to
be the accept button, this stopped the beep but also stopped the keyevent
from firing too.

I also set up a click event for this default button to send a processcmdkey
or processkeydialog call so that the keyevent would fire as part of the click
event, no luck though.

I have tried to override processcmdkey and processkeydialog both to no
avail, I cannot seem to capture the event the acceptbutton event hits.

My target is to stop the beep AND still process the keyevent, thank you in
advance for any help.
 
A

Armin Zingler

js1300 said:
Hello, I'm working on a set of forms inherited from the windows
form. Within these forms I often use keyevents to perform certain
functions i.e. 'return/enter in a textbox' to submit a value to a
routine, perform a search or just to send a tab.

As I have no one button on the form that I would always want to fire
a click when I hit enter I have the acceptbutton set to nothing.
This causes the obvious problem of the windows default beep sounding
everytime the key is pressed.

I have attempted to change the inherited form, setting a default
button to be the accept button, this stopped the beep but also
stopped the keyevent from firing too.

I also set up a click event for this default button to send a
processcmdkey or processkeydialog call so that the keyevent would
fire as part of the click event, no luck though.

I have tried to override processcmdkey and processkeydialog both to
no avail, I cannot seem to capture the event the acceptbutton event
hits.

My target is to stop the beep AND still process the keyevent, thank
you in advance for any help.


In the textbox' keypress event, set e.handled = true. It beeps because CR is
not handled.

Private Sub TextBox1_KeyPress( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress

If e.KeyChar = ControlChars.Cr Then
e.Handled = True
End If
End Sub

Note, it doesn't beep because it's the *key* that isn't handled. It beeps
because the *char* (CR) is not handled (therefore keypress, not keydown).
Means, same happens if you press Ctrl+A to enter chr(1) into the textbox.
You would have to handle this too.


Armin
 
J

Juan Pedro Gonzalez

Hi,

You can set "e.handled = True" in the event keyPress and this way you'll
silence the beep.

As a proof of concept:

One button and one TextBox:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar.Equals(ControlChars.Cr) Then
Me.Button1_Click(Nothing, Nothing)
e.Handled = True
End If
End Sub

Best wishes,

Juan Pedro González
 
G

Guest

Excellent stuff, I have integrated that into my inherited textboxes and I now
have the action I want. Thank you both.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top