Home All Groups Group Topic Archive Search About
Author
27 Jun 2005 1:43 PM
js1300
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.

Author
27 Jun 2005 2:09 PM
Armin Zingler
Show quote Hide quote
"js1300" <js1***@discussions.microsoft.com> schrieb
> 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
Author
27 Jun 2005 2:25 PM
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
Author
27 Jun 2005 3:39 PM
js1300
Excellent stuff, I have integrated that into my inherited textboxes and I now
have the action I want.  Thank you both.