We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

ShowDialog fired by Enter key causes Beep

davidabyford
davidabyford asked
on
Medium Priority
1,784 Views
Last Modified: 2008-02-01
If I try to open a form using the ShowDialog method from a controls Keydown event, a beep is sounded as the form is opened even if I supress the keystroke first.


        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            With New frm
                .ShowDialog()
            End With
        End If
Comment
Watch Question

CERTIFIED EXPERT

Commented:
what code is there in the form_Load?
CERTIFIED EXPERT

Commented:
e.SuppressKeyPress   this makes the beep!!! when you hold any button the buffer gets full and makes a system beep

 If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            With New frm
                .ShowDialog()
            End With
           e.SuppressKeyPress = False
        End If

Author

Commented:
Hi Mike

How does the e.SuppressKeyPress = False help, the beep occurrs on the showdialog line.
Form has nothing in it's open event, in fact form has no code at all.
Enter or Return keys seem to be the problem, other keytypes like function key's work silently.
CERTIFIED EXPERT

Commented:
and if you do this

 If e.KeyCode = Keys.Enter Then
                        With New frm
                .ShowDialog()
            End With
                 End If

What happens?

Author

Commented:
beep
CERTIFIED EXPERT

Commented:
wow!!!and if you add a button to that form and use the button click to open a new form do you hear the beep?

Author

Commented:
Nope, beep only occurrs opening form modally when trapping Enter/Return

CERTIFIED EXPERT

Commented:
try

 If e.KeyCode = Keys.Enter Then
          e.keycode = 0
             With New frm
                .ShowDialog()
            End With
End If

Author

Commented:
Like I would do in VB6, unfortunately keycode property is readonly in .NET
CERTIFIED EXPERT

Commented:
forgot!! sorry
CERTIFIED EXPERT

Commented:
try using the key up event
CERTIFIED EXPERT

Commented:
or keypress
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Right...use the KeyPress event and then set the Handled() property to True:

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            e.Handled = True
            With New frm
                .ShowDialog()
            End With
        End If
    End Sub

Author

Commented:
That does work and I use it VB6 but hadn't considered in this context because I also use the textbox to handles certain function key presses as well as the enter. BUT.....

Does mean I could split the logic between keydopwn and keypress, keydown won't be handling enter any more, so should be fine.

Thanks, will try this tonight.
CERTIFIED EXPERT

Commented:
great
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
There shouldn't be a problem splitting the logic between KeyDown/KeyUp and KeyPress (unless you duplicate keys in them).
CERTIFIED EXPERT

Commented:
even if you dup them youll have to actions for that button thats also fine. So your actions should not be duped

Author

Commented:
OR ....  Just keep my original keydown code(with or without keysupress) and put e.handled in keyPress
Simple as that
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
I agree and think that would be the simplest:

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            e.Handled = True
        End If
    End Sub

...and do the actual code in the KeyDown() event...

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
what I meant for the keypress was :

        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            e.Handled = True
        End If

surpressing all keystrokes would not be good :-)
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Exactly...  =)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.