ShowDialog fired by Enter key causes Beep

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
davidabyfordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
what code is there in the form_Load?
0
Mikal613Commented:
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
0
davidabyfordAuthor 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.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Mikal613Commented:
and if you do this

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

What happens?
0
davidabyfordAuthor Commented:
beep
0
Mikal613Commented:
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?
0
davidabyfordAuthor Commented:
Nope, beep only occurrs opening form modally when trapping Enter/Return

0
Mikal613Commented:
try

 If e.KeyCode = Keys.Enter Then
          e.keycode = 0
             With New frm
                .ShowDialog()
            End With
End If
0
davidabyfordAuthor Commented:
Like I would do in VB6, unfortunately keycode property is readonly in .NET
0
Mikal613Commented:
forgot!! sorry
0
Mikal613Commented:
try using the key up event
0
Mikal613Commented:
or keypress
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
davidabyfordAuthor 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.
0
Mikal613Commented:
great
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
There shouldn't be a problem splitting the logic between KeyDown/KeyUp and KeyPress (unless you duplicate keys in them).
0
Mikal613Commented:
even if you dup them youll have to actions for that button thats also fine. So your actions should not be duped
0
davidabyfordAuthor Commented:
OR ....  Just keep my original keydown code(with or without keysupress) and put e.handled in keyPress
Simple as that
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davidabyfordAuthor 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 :-)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Exactly...  =)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.